Advertisement
Korotkodul

ЗОШ ХЭШЫ ОК

Jan 11th, 2022 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 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. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. using db = double;
  15. void cv(vector <int> &v){
  16. for (auto x: v) cout<<x<<' ';
  17. cout<<"\n";
  18. }
  19.  
  20. void cvl(vector <ll> &v){
  21. for (auto x: v) cout<<x<<' ';
  22. cout<<"\n";
  23. }
  24.  
  25.  
  26. void cvv(vector <vector <int> > &v){
  27. for (auto x: v) cv(x);
  28. cout<<"\n";
  29. }
  30.  
  31. int n;
  32.  
  33. struct sct{
  34. int l,r;
  35. };
  36.  
  37. void mk(sct &x){
  38. cin>>x.l>>x.r;
  39. x.l--;
  40. x.r--;
  41. }
  42.  
  43. sct a,b;
  44.  
  45. ll p = 29, mod = 1e9 + 7;
  46.  
  47. vector <ll> hsh, pf, lvp;
  48.  
  49. ll H(sct x){
  50. ll res;
  51. res = pf[x.r];
  52. ll k = x.r - x.l + 1;
  53. if (x.l > 0){
  54. res -= pf[x.l-1] * lvp[k] % mod;
  55. }
  56. if (res < 0) res += mod;
  57. return res;
  58. }
  59.  
  60. ll nm(char x){
  61. return x - 'a' + 1;
  62. }
  63.  
  64. void sh(sct x){
  65. cout<<x.l<<' '<<x.r<<"\n";
  66. }
  67.  
  68. ll chk(ll x){
  69. if (x < 0){
  70. return x + mod;
  71. }
  72. return x;
  73. }
  74. В ЭТОЙ ЗАДАЧЕ СРАВНИВАЮТСЯ ПОДСТРОКИ
  75. int main()
  76. {
  77. ios::sync_with_stdio(0);
  78. cin.tie(0);
  79. cout.tie(0);
  80. string S; cin>>S;
  81. n = S.size();
  82. lvp.resize(n);
  83. hsh.resize(n);
  84. pf.resize(n);
  85. int t; cin>>t;
  86.  
  87. lvp[0] = 1;
  88.  
  89. for (int i = 1; i <n;++i){
  90. lvp[i] = lvp[i-1] * p % mod;
  91. }
  92.  
  93. for (int i = 0; i < n;++i){
  94. hsh[i] = nm(S[i]) * lvp[n - i - 1] % mod;
  95. }
  96. pf[0] = nm(S[0]);
  97. for (int i = 1; i<n;++i){
  98. pf[i] = pf[i-1] * p % mod + nm(S[i]);
  99. pf[i] %= mod;
  100. }
  101. /*cout<<"lvp\n";
  102. cvl(lvp);
  103. cout<<"pf\n";
  104. cvl(pf);
  105. cout<<"hsh\n";
  106. cvl(hsh);*/
  107.  
  108.  
  109.  
  110. for (int i = 0; i < t;++i){
  111. mk(a); mk(b);
  112. /*cout<<"a b\n";
  113. sh(a); sh(b);
  114. cout<<"H(a) = "<<H(a)<<"\n";
  115. cout<<"H(b) = "<<H(b)<<"\n";*/
  116. if (H(a) == H(b)){
  117. cout<<"Yes\n";
  118. }
  119. else{
  120. cout<<"No\n";
  121. }
  122. }
  123. }
  124. /*
  125. trololo
  126. 3
  127. 1 7 1 7
  128. 3 5 5 7
  129. 1 1 1 5
  130.  
  131. */
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement