Advertisement
Korotkodul

ЗОШ хэши сравнение подстрок OK

Jan 13th, 2022 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31. string S;
  32.  
  33. int n;
  34.  
  35. vector <ll> pf, lvp;
  36.  
  37. const int p = 29, mod = 1e9 + 7;
  38.  
  39.  
  40. int nm(char x){
  41. return x - 'a' + 1;
  42. }
  43.  
  44. ll H(int l, int r){
  45. ll k = r - l + 1;
  46. ll res = pf[r];
  47. if (l > 0){
  48. res -= pf[l-1] * lvp[k] % mod;
  49. }
  50. if (res < 0) res += mod;
  51. return res;
  52. }
  53.  
  54.  
  55.  
  56. int l1, r1, l2, r2;
  57.  
  58. int cmp(){
  59. int lf, rt, md;
  60. //бинописком ищем длину наибольшего общего префикса
  61. lf = 0;
  62. rt = min(r2 - l2, r1 - l1);
  63. //cout<<"lf rt = "<<lf<<' '<<rt<<"\n";
  64. //cout<<"l r = "<<l1<<' '<<r1<<' '<<l2<<' '<<r2<<"\n";
  65. while (rt - lf > 1){
  66. md = (lf + rt) / 2;
  67. ll a,b;
  68. a = H(l1, l1 + md);
  69. b = H(l2, l2 + md);
  70. //cout<<"md = "<<md<<"\n";
  71. //cout<<"a b = "<<a<<' '<<b<<"\n";
  72. if (a == b){
  73. lf = md;
  74. }
  75. else rt= md;
  76. }
  77. //cout<<"rt = "<<rt<<"\n";
  78. rt--;
  79. if (rt<0)rt=0;
  80. while (S[l1+rt]==S[l2+rt] && l1 + rt <= r1 && l2 + rt <= r2) rt++;
  81. //cout<<"rt = "<<rt<<"\n";
  82. if (l1 + rt > r1 && l2 + rt > r2){
  83. return 0;
  84. }
  85. if (l1 + rt > r1) {
  86. return -1;
  87. }
  88. if (l2 + rt > r2){
  89. return 1;
  90. }
  91. if (S[l1 + rt] > S[l2 + rt]){
  92. //cout<<"four\n";
  93. return 1;
  94. }
  95. if (S[l1 + rt] < S[l2 + rt]){
  96. //cout<<"five\n";
  97. return -1;
  98. }
  99. if (S[l1 + rt] == S[l2 + rt]){
  100. //cout<<"six\n";
  101. return 0;
  102. }
  103. return -1;
  104. }
  105.  
  106. int main()
  107. {
  108. ios::sync_with_stdio(0);
  109. cin.tie(0);
  110. cout.tie(0);
  111. cin>>S;
  112. n = S.size();
  113. lvp.assign(n+5, 1);
  114. for (int i = 1; i<=n;++i) lvp[i] = lvp[i-1] * p % mod;
  115. pf.assign(n, nm(S[0]));
  116. for (int i =1;i<n;++i){
  117. pf[i] = (pf[i-1] * p % mod + nm(S[i])) % mod;
  118. }
  119. int q; cin>>q;
  120. int ans;
  121. for (int i=0;i<q;++i){
  122. cin>>l1>>r1>>l2>>r2;
  123. ans = cmp();
  124. //cout<<"ans = "<<ans<<"\n";
  125. cout<<ans<<"\n";
  126. }
  127. }
  128. /*
  129. abcabcabc
  130.  
  131. aaaaabbbbbccccc
  132.  
  133.  
  134. abbaabba
  135. */
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement