Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int BASE = 1000000000 + 7;
  6. const int MODULO = 1000000000 + 31;
  7.  
  8. inline int add(int a, int b)
  9. {
  10. return (a + b) % MODULO;
  11. }
  12.  
  13. inline int subtract(int a, int b)
  14. {
  15. return add(a, MODULO - b);
  16. }
  17.  
  18. inline int multiply(long long a, long long b)
  19. {
  20. return (int)((a * b) % MODULO);
  21. }
  22.  
  23. struct HashLeft
  24. {
  25. vector <int> base_powers;
  26.  
  27. void init_hash(int size)
  28. {
  29. base_powers.resize(size, 0);
  30. base_powers[0] = 1;
  31. for (int i = 1; i < size; ++i)
  32. base_powers[i] = multiply(base_powers[i - 1], BASE);
  33. }
  34.  
  35. vector <int> calculate_hashes(string &s)
  36. {
  37. int size = s.size();
  38. vector<int> hashes(size + 1, 0);
  39.  
  40. for (int i = 0; i < size; ++i)
  41. hashes[i + 1] = add(hashes[i], multiply(s[i], base_powers[i]));
  42.  
  43. return hashes;
  44. }
  45.  
  46. int calculate_hash(string & s)
  47. {
  48. return calculate_hashes(s)[s.size()];
  49. }
  50.  
  51. // [start..end)
  52. int get_hash(vector <int> &hashes, int start, int end)
  53. {
  54. return subtract(hashes[end], hashes[start]);
  55. }
  56.  
  57. vector <int> find(string &s, string &t)
  58. {
  59. int size = max(s.size(), t.size()) + 1;
  60. init_hash(size);
  61.  
  62. vector <int> s_hashes = calculate_hashes(s);
  63.  
  64. int t_hash = calculate_hash(t);
  65. int t_size = t.size();
  66.  
  67. vector <int> positions;
  68.  
  69. for (int i = 0; i + t_size <= s.size(); ++i)
  70. {
  71. int s_hash = get_hash(s_hashes, i, i + t_size);
  72. if (multiply(t_hash, base_powers[i]) == s_hash)
  73. positions.push_back(i);
  74. }
  75.  
  76. return positions;
  77. }
  78. };
  79.  
  80. struct HashRight
  81. {
  82. vector <int> base_powers;
  83.  
  84. void init_hash(int size)
  85. {
  86. base_powers.resize(size, 0);
  87. base_powers[0] = 1;
  88. for (int i = 1; i < size; ++i)
  89. base_powers[i] = multiply(base_powers[i - 1], BASE);
  90. }
  91.  
  92. vector <int> calculate_hashes(string &s)
  93. {
  94. int size = s.size();
  95. vector<int> hashes(size + 1, 0);
  96.  
  97. for (int i = 0; i < size; ++i)
  98. hashes[i + 1] = add(multiply(hashes[i], BASE), s[i]);
  99.  
  100. return hashes;
  101. }
  102.  
  103. int calculate_hash(string &s)
  104. {
  105. return calculate_hashes(s)[s.size()];
  106. }
  107.  
  108. int get_hash(vector<int> & hashes, int start, int end)
  109. {
  110. return subtract(
  111. hashes[end],
  112. multiply(hashes[start], base_powers[end - start])
  113. );
  114. }
  115.  
  116. vector <int> find(string &s, string &t)
  117. {
  118. int size = max(s.size(), t.size()) + 1;
  119. init_hash(size);
  120.  
  121. vector <int> s_hashes = calculate_hashes(s);
  122.  
  123. int t_hash = calculate_hash(t);
  124. int t_size = t.size();
  125. vector <int> positions;
  126.  
  127. for (int i = 0; i + t_size <= s.size(); ++i)
  128. {
  129. int s_hash = get_hash(s_hashes, i, i + t_size);
  130. if (t_hash == s_hash)
  131. positions.push_back(i);
  132. }
  133.  
  134. return positions;
  135. }
  136. };
  137.  
  138. int main()
  139. {
  140. int n, m;
  141. scanf("%d %d", &n, &m);
  142. vector <int> a(n);
  143. string b = "";
  144.  
  145. for (int i = 0; i < n; ++i)
  146. scanf("%d", &a[i]);
  147. for (int i = 0; i < m; ++i)
  148. {
  149. char c;
  150. cin >> c;
  151. b += c;
  152. }
  153.  
  154. HashRight hash;
  155. int ans = 0;
  156. for (int i = 0; i < n; ++i)
  157. {
  158. string x = to_string(a[i]);
  159. vector <int> positions = hash.find(b, x);
  160. ans += positions.size();
  161. }
  162.  
  163. printf("%d", ans);
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement