Guest User

CSCARE-solution

a guest
Apr 5th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. const ll mod = 1e9 + 7;
  6.  
  7. #define speedup_IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
  8. #define forn(i,a,n) for(ll i = a; i < n; i++)
  9. #define ford(i,n,a) for(ll i = n-1; i >= a; i--)
  10. #define pb push_back
  11. #define lb lower_bound
  12. #define ub upper_bound
  13. #define pll pair<ll,ll>
  14. #define all(a) (a).begin(),(a).end()
  15. #define reset(a,x) memset(a,x,sizeof(a))
  16.  
  17. ll mypow(ll base, ll exp) {ll res = 1; while (exp) {if (exp & 1) res = (res * base) % mod; exp >>= 1, base = (base * base) % mod;} return res;}
  18. ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
  19. ll mult(ll a, ll b) { if (a < 0) {a += mod;} if (b < 0) {b += mod;} return ((a % mod) * (b % mod)) % mod; }
  20. ll add(ll a, ll b) { if (a < 0) {a += mod;} if (b < 0) {b += mod;} return (a % mod + b % mod) % mod; }
  21. ll sub(ll a, ll b) { if (a < 0) {a += mod;} if (b < 0) {b += mod;} return (a % mod + mod - b % mod) % mod; }
  22.  
  23. #define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
  24.  
  25. void err(istream_iterator<string> it) {}
  26. template<typename T, typename... Args>
  27. void err(istream_iterator<string> it, T a, Args... args) {
  28. cerr << *it << " = " << a << " | ";
  29. err(++it, args...);
  30. }
  31.  
  32. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  33.  
  34. const ll inf = 1e18;
  35. const int nax = 1e5 + 5;
  36.  
  37. vector<int> prefix_function(vector<int> &str)
  38. {
  39. int n = str.size();
  40. vector<int>pi(n);
  41.  
  42. for (int i = 1; i < n; i++)
  43. {
  44. int j = pi[i - 1];
  45. while (j > 0 && str[j] != str[i])
  46. j = pi[j - 1];
  47. if (str[i] == str[j])
  48. j++;
  49. pi[i] = j;
  50. }
  51.  
  52. return pi;
  53. }
  54.  
  55. int count_subarrays(vector<int> &password, vector<int> &entered, int faultiness)
  56. {
  57. vector<int>pat;
  58. int n = password.size(), m = entered.size();
  59. for (int i = 1; i < n; i++)
  60. {
  61. pat.pb(password[i] - password[i - 1]);
  62. }
  63.  
  64. pat.pb(1e9);
  65.  
  66. for (int i = 1; i < m; i++)
  67. {
  68. pat.pb(entered[i] - entered[i - 1]);
  69. }
  70.  
  71. vector<int>pi = prefix_function(pat);
  72.  
  73. int ans = 0;
  74. for (int i = 2 * (n - 1) ; i <= min(m + 2 * (n - 1) - 1, n + m - 2); i++)
  75. {
  76. if (pi[i] == n - 1 && abs(password[0] - entered[i - (2 * (n - 1))]) <= faultiness)
  77. {
  78. ans++;
  79. }
  80. }
  81.  
  82. return ans;
  83. }
  84.  
  85. int main()
  86. {
  87. int t = 1;
  88. cin >> t;
  89. while (t--)
  90. {
  91. int faultiness, n, m;
  92. cin >> faultiness;
  93.  
  94. cin >> n;
  95. vector<int> password(n);
  96. for (int i = 0; i < n; i++)
  97. {
  98. cin >> password[i];
  99. }
  100.  
  101. cin >> m;
  102. vector<int> entered(m);
  103. for (int i = 0; i < m; i++)
  104. {
  105. cin >> entered[i];
  106. }
  107.  
  108. cout << count_subarrays(password, entered, faultiness) << endl;
  109.  
  110. }
  111. return 0;
  112. }
Add Comment
Please, Sign In to add comment