Advertisement
bibaboba12345

Untitled

Mar 27th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. // clang-format off
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <bitset>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <random>
  9. #include <map>
  10. #include <string>
  11. #include <set>
  12. #include <deque>
  13. #include <string>
  14. #include <cassert>
  15.  
  16. #pragma GCC optimize("Ofast")
  17.  
  18. using namespace std;
  19. const int N = 1e6 + 7, MOD = 998244353, M = 139;
  20. const long double EPS = 1e-10, PI = acos(-1);
  21.  
  22. using ll = long long;
  23.  
  24. ll binpow(ll a, int step) {
  25. if (step == 0) {
  26. return 1;
  27. }
  28. ll h = binpow(a, step / 2);
  29. h = (h * h) % MOD;
  30. if (step % 2) {
  31. h = (h * a) % MOD;
  32. }
  33. return h;
  34. }
  35.  
  36. ll pows[N];
  37. ll q = 149;
  38.  
  39. struct Hash {
  40. ll hsh = 0;
  41. int len;
  42. void pop_back(int a) {
  43. hsh -= ((a + 1) * pows[len - 1]) % MOD;
  44. if (hsh < 0) {
  45. hsh += MOD;
  46. }
  47. len--;
  48. }
  49. void push_front(int a) {
  50. hsh *= q;
  51. hsh += a + 1;
  52. hsh %= MOD;
  53. len++;
  54. }
  55. };
  56.  
  57. Hash fwd[N], fwd2[N];
  58.  
  59. int a[N], b[N], n, m, q1, bwd[N];
  60.  
  61.  
  62. int z[N];
  63.  
  64. vector<int> s;
  65.  
  66. bool good[N];
  67.  
  68. void zfun() {
  69. int n = s.size();
  70. z[0] = 0;
  71. int L = 0, R = 0;
  72. for (int i = 1; i < n; i++) {
  73. z[i] = max(0, min(z[i - L], R - i+1));
  74. while (i + z[i] < n && s[i + z[i]] == s[z[i]]) {
  75. z[i]++;
  76. }
  77. if (i + z[i] - 1 > R) {
  78. L = i;
  79. R = i + z[i] - 1;
  80. }
  81. }
  82. }
  83.  
  84. signed main() {
  85. #ifdef _DEBUG
  86. freopen("input.txt", "r", stdin);
  87. #else
  88. std::ios::sync_with_stdio(false);
  89. cin.tie(0);
  90. #endif
  91. pows[0] = 1;
  92. for (int i = 1; i < N; i++) {
  93. pows[i] = (q * pows[i - 1]) % MOD;
  94. }
  95. cin >> n >> m >> q1;
  96. for (int i = 0; i < m; i++) {
  97. cin >> a[i];
  98. }
  99. for (int i = 0; i < n; i++) {
  100. cin >> b[i];
  101. }
  102. for (int i = m - 1; i >= 0; i--) {
  103. if (i + q1 >= m) {
  104. }
  105. else {
  106. fwd[i] = fwd[i + q1];
  107. fwd[i].push_front((a[i] - a[i + q1] + M) % M);
  108. }
  109. }
  110. for (int i = n - 1; i >= 0; i--) {
  111. if (i + q1 >= n) {
  112. }
  113. else {
  114. fwd2[i] = fwd2[i + q1];
  115. fwd2[i].push_front((b[i] - b[i+q1] + M) % M);
  116. if (fwd2[i].len >= m / q1) {
  117. int i1 = i + q1 * (fwd2[i].len - 1);
  118. fwd2[i].pop_back((b[i1] - b[i1 + q1] + M) % M);
  119. }
  120. }
  121. }
  122. s.clear();
  123. for (int i = 0; i < q1; i++) {
  124. s.push_back(fwd[i].hsh);
  125. }
  126. int ln = s.size() + 1;
  127. s.push_back(-1);
  128. for (int i = 0; i < n; i++) {
  129. s.push_back(fwd2[i].hsh);
  130. }
  131. zfun();
  132. for (int i = ln; i < s.size(); i++) {
  133. if (z[i] >= q1) {
  134. good[i - ln] = 1;
  135. //cout << i - ln << " ";
  136. }
  137. }
  138. s.clear();
  139. for (int i = 0; i < q1; i++) {
  140. s.push_back(a[i]);
  141. }
  142. s.push_back(-1);
  143. for (int i = 0; i < n; i++) {
  144. if (i - q1 >= 0) {
  145. s.push_back((b[i] - b[i - q1] + M) % M);
  146. }
  147. else {
  148. s.push_back(-1);
  149. }
  150. }
  151. zfun();
  152. for (int i = ln; i < s.size(); i++) {
  153. if (z[i] >= q1) {
  154. good[i - ln] &= 1;
  155. }
  156. else {
  157. good[i - ln] = 0;
  158. }
  159. }
  160. for (int i = q1; i < n; i++) {
  161. if (good[i]) {
  162. cout << i - q1 + 1;
  163. return 0;
  164. }
  165. }
  166. cout << -1;
  167. }
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement