Advertisement
Korotkodul

hash

Jun 9th, 2022 (edited)
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 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. void cvs(vector <string>  v){
  38.     for (auto a: v){
  39.         cout<<a<<"\n";
  40.     }
  41. }
  42.  
  43. int f(char x){
  44.     return x - 'a' + 1;
  45. }
  46.  
  47. ll mod = 1e9+7, p=37;
  48.  
  49. int main()
  50. {
  51.     ios::sync_with_stdio(0);
  52.     cin.tie(0);
  53.     cout.tie(0);
  54.     string T,S;
  55.     cin>>S>>T;
  56.     int Sn = S.size(), Tn = T.size();
  57.     vector <ll> powP(max(Sn,Tn), 1);
  58.     for (int i = 1; i < powP.size();++i){
  59.         powP[i] = powP[i-1] * p;
  60.         powP[i] %= mod;
  61.     }
  62.    
  63.     vector <ll> Shp(Sn), Thp(Tn);
  64.     Shp[0] = f(S[0]);
  65.     for (int i=1;i<Sn;++i){
  66.         Shp[i] = Shp[i-1] + f(S[i]) * powP[i];
  67.         Shp[i] %= mod;
  68.     }
  69.    
  70.     Thp[0] = f(T[0]);
  71.     for (int i=1;i<Tn;++i){
  72.         Thp[i] = Thp[i-1] + f(T[i]) * powP[i];
  73.         Thp[i] %= mod;
  74.     }
  75.    
  76.     vector <int> ans;
  77.     if (Shp[Tn-1] == Thp[Tn-1]) ans = {0};
  78.     for (int i = 1; i < Sn - Tn + 1; ++i){
  79.         if (Shp[i + Tn - 1] - Shp[i - 1] == Thp[Tn-1] * powP[i] % mod){//%mod - так как мы перемножаем и число может превысить mod
  80.             ans.push_back(i);
  81.         }
  82.     }
  83.     cv(ans);
  84. }
  85. /*
  86. aaaaaaaaaaaaaaaaaaaaaaaaaaaa
  87. a
  88. 0 1 2 3 4 5 6 9 11 13 15 18 19 22 24 25
  89.  
  90. HERE IS THE MISTAKE!!!
  91. */
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement