Guest User

Untitled

a guest
Apr 5th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef vector<int> vi;
  5. typedef vector<ll> vl;
  6. typedef pair<int,int> pii;
  7. typedef pair<ll,ll> pll;
  8. #define pi 3.141592653589793
  9. #define mod 1000000007
  10. #define pb push_back
  11. #define all(v) v.begin(),v.end()
  12. #define tc int t;cin>>t;while(t--)
  13. #define pqmax priority_queue<int>
  14. #define pqmin priority_queue<int,vi,greater<int>>
  15. #define fast_io ios_base::sync_with_stdio(0), cin.tie(NULL)
  16. #define tc_g int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
  17. #define case_g "Case #"<<ti<<": "
  18. #include <ext/pb_ds/assoc_container.hpp>
  19. #include <ext/pb_ds/tree_policy.hpp>
  20. using namespace __gnu_pbds;
  21. typedef tree<int, null_type, greater_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
  22. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  23. typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
  24.  
  25. int ans = 0;
  26. vi a, b;
  27.  
  28. vi prefix_array(vi& pat) {
  29.     int len = 0;
  30.     int n = pat.size();
  31.     vi lps(n);
  32.     lps[0] = 0;
  33.     int i = 1;
  34.     while (i < n) {
  35.         if (pat[i] == pat[len]) {
  36.             len++;
  37.             lps[i] = len;
  38.             i++;
  39.         }
  40.         else if (len != 0)
  41.             len = lps[len - 1];
  42.         else {
  43.             lps[i] = 0;
  44.             i++;
  45.         }
  46.     }
  47.     return lps;
  48. }
  49.  
  50. void KMPSearch(vi& pat, vi& pass, int x)
  51. {
  52.     int M = pat.size();
  53.     int N = pass.size();
  54.     vi lps = prefix_array(pat);
  55.     int i = 0, j = 0;
  56.     while (i < N) {
  57.         if (pat[j] == pass[i]) {
  58.             j++;
  59.             i++;
  60.         }
  61.         if (j == M) {
  62.             if (abs(b[i - j] - a[0]) <= x)
  63.                 ans++;
  64.             j = lps[j - 1];
  65.         }
  66.         else if (i < N && pat[j] != pass[i]) {
  67.             if (j != 0)
  68.                 j = lps[j - 1];
  69.             else
  70.                 i = i + 1;
  71.         }
  72.     }
  73. }
  74.  
  75. int main() {
  76.     fast_io;
  77.     tc {
  78.         int x, n, m;
  79.         cin >> x >> n;
  80.         a.resize(n);
  81.         for (int i = 0;i < n;i++)
  82.             cin >> a[i];
  83.         cin >> m;
  84.         b.resize(m);
  85.         for (int i = 0;i < m;i++)
  86.             cin >> b[i];
  87.         vi pat(n - 1);
  88.         for (int i = 0;i < n - 1;i++)
  89.             pat[i] = a[i + 1] - a[i];
  90.         vi pass(m - 1);
  91.         for (int i = 0;i < m - 1;i++)
  92.             pass[i] = b[i + 1] - b[i];
  93.         ans = 0;
  94.         if (n > 1) {
  95.             KMPSearch(pat, pass, x);
  96.         }
  97.         else {
  98.             for (auto p : b)
  99.                 if (abs(p - a[0]) <= x)
  100.                     ans++;
  101.         }
  102.         cout << ans << '\n';
  103.     }
  104. }
Add Comment
Please, Sign In to add comment