Korotkodul

хэши A

Feb 2nd, 2022 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 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.  
  32. void cvb(vector <bool> v){
  33. for (bool x: v) cout<<x<<' ';
  34. cout<<"\n";
  35. }
  36.  
  37. const ll mod= 1e9+7, k = 29;
  38.  
  39. vector <string> str;
  40.  
  41. vector <ll> kpow;
  42.  
  43. vector <vector <ll> > hp; //hash-pref
  44.  
  45. ll h(int l, int r, int id){//какая строка
  46. // cout<<"l r = "<<l<<' '<<r<<"\n";
  47. //cout<<"id = "<<id<<"\n";
  48. //cout<<"l r = "<<l<<' '<<r<<"\n";
  49. ll res = hp[id][r];
  50. //cout<<"a = "<<res<<"\n";
  51. if (l > 0){
  52. //cout<<"b= "<<(hp[id][l-1] * kpow[r - l + 1] % mod)<<"\n";
  53. res -= hp[id][l-1] * kpow[r - l + 1] % mod;
  54. }
  55. //cout<<"a - b = "<< res<<"\n";
  56. return (res + mod) % mod;
  57. }
  58.  
  59. ll num(char x){
  60. return x - 'a' + 1;
  61. }
  62.  
  63. int main()
  64. {
  65. /* ios::sync_with_stdio(0);
  66. cin.tie(0);
  67. cout.tie(0);*/
  68.  
  69. int q = 2;
  70. str.resize(q);
  71. int mx_sz = -1;
  72. for (int i = 0;i<q;++i){
  73. cin>>str[i];
  74. int sz = str[i].size();
  75. mx_sz = max(mx_sz, sz);
  76. }
  77. hp.resize(q);
  78. kpow.assign(mx_sz, 1);
  79. //cout<<"str\n";
  80. //for (auto s: str) cout<<s<<"\n";
  81. //cout<<"hash\n";
  82. for (int i=1;i<mx_sz;++i){
  83. kpow[i] = kpow[i-1] * k % mod;
  84. }
  85. //cout<<"kpow\n";
  86. //cvl(kpow);
  87. for (int i = 0; i<q;++i){
  88. //cout<<"i = "<<i<<"\n";
  89. hp[i].resize(str[i].size());
  90. hp[i][0] = num(str[i][0]);
  91. for (int j = 1; j < str[i].size(); ++j){
  92. // cout<<j<<' ';
  93. hp[i][j] = ( hp[i][j-1] * k % mod + num(str[i][j]) ) % mod;
  94. }
  95. //cout<<"\n";
  96. }
  97.  
  98. //cout<<"hash\n";
  99. //for (auto g: hp) cvl(g);
  100. //cout<<"COUNT\n";
  101. vector <int> ans;
  102. for (int i = 0; i <= str[0].size() - str[1].size();++i){
  103. //cout<<"i = "<<i<<"\n";
  104. //cout<<"h(S) h(T) = "<<h(i, i + str[1].size() - 1, 1)<<' '<<h(0, str[1].size()-1, 0)<<"\n";
  105. if (h(i, i + str[1].size() - 1, 0) == h(0, str[1].size()-1, 1) ){
  106. ans.push_back(i);
  107. }
  108. //cout<<"\n\n";
  109. }
  110. //cout<<"ans\n";
  111. cv(ans);
  112. }
  113. /*
  114. ababbababa
  115. aba
  116. */
  117.  
Add Comment
Please, Sign In to add comment