Advertisement
welleyth

99. Поиск подстроки

Nov 5th, 2020
2,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. /********************
  2. * Problem:
  3. * Theme:
  4. */
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. #define int long long
  10. #define mp make_pair
  11. #define pb push_back
  12. #define ALL(A) A.begin(),A.end()
  13. #define RALL(A) A.rbegin(),A.rend()
  14. #define FOR(i,a,b) for(int i=(a);i<(b);i++)
  15. #define RFOR(i,a,b) for(int i=(a);i>=(b);i--)
  16. #define SZ(A) A.size()
  17. #define f first
  18. #define s second
  19. #define endl '\n'
  20.  
  21. typedef vector<int> VI;
  22. typedef vector<pair<int,int> > VII;
  23. typedef vector<VI> VVI;
  24. typedef pair<int,int> pii;
  25. typedef pair<int,pair<int,int> > pip;
  26. typedef pair<pair<int,int>,pair<int,int> > ppp;
  27. typedef pair<pair<int,int>,int> ppi;
  28.  
  29. const long double PI = acos(-1);
  30. const int INF = 72340172838076673;
  31. const int MOD = (int)1e9+7;
  32. const int P = 3;
  33. //const int N = 1e6+1;
  34. const long double eps = 1e-8;
  35.  
  36. const int DX[] = {1,1,2,2,-1,-1,-2,-2};
  37. const int DY[] = {-2,2,1,-1,-2,2,1,-1};
  38.  
  39. int bpow(int a,int b)
  40. {
  41.     if(b==0)
  42.         return 1;
  43.     if(b%2==0)
  44.     {
  45.         int t=bpow(a,b/2);
  46.         return t*t;
  47.     }
  48.     return a*bpow(a,b-1);
  49. }
  50.  
  51. bool isPrime(int n)
  52. {
  53.     if(n==1)
  54.         return false;
  55.     for(int i=2; i*i<=n; i++)
  56.     {
  57.         if(n%i==0)
  58.             return false;
  59.     }
  60.     return true;
  61. }
  62.  
  63. bool isPal(string s)
  64. {
  65.     if(s.size()==0)
  66.         return false;
  67.     for(int i=0,j=s.size()-1;i<j;i++,j--)
  68.     {
  69.         if(s[i]!=s[j])
  70.             return false;
  71.     }
  72.     return true;
  73. }
  74.  
  75. int amountOfDividers(int n)
  76. {
  77.     if(n==1)
  78.         return 1;
  79.     int counter=2 + (sqrt(n)==(int)sqrt(n));
  80.     for(int i=2;i<sqrt(n);i++)
  81.     {
  82.         counter+=2*(n%i==0);
  83.     }
  84.     return counter;
  85. }
  86.  
  87. int Hash[50001];
  88. int PP[50001];
  89. int N;
  90.  
  91. bool areEqual(int l,int r,int HASH)
  92. {
  93.     int K = Hash[r] - (l == 0 ? 0ll : Hash[l - 1]);
  94.     while(K < 0)
  95.         K+=MOD;
  96.     K = (K * PP[N-l+1])%MOD;
  97.     //cout << K << " " << HASH << " " << l << " " << r << "\n";
  98.     return K == HASH;
  99. }
  100.  
  101. void solve_case()
  102. {
  103.     string s;
  104.     cin >> s;
  105.  
  106.     N = s.size();
  107.  
  108.     Hash[0] = s[0];
  109.  
  110.     PP[0] = 1;
  111.  
  112.     for(int i=1;i<s.size();i++)
  113.     {
  114.         PP[i] = (PP[i-1] * P)%MOD;
  115.         Hash[i] = (Hash[i-1]+ s[i]*PP[i])%MOD;
  116.     }
  117.  
  118. //    cout << "Hash array : ";
  119. //
  120. //    for(int i=0;i<s.size();i++)
  121. //    {
  122. //        cout << Hash[i] << " ";
  123. //    }
  124. //
  125. //    cout << "\n";
  126.  
  127.     string ss;
  128.     cin >> ss;
  129.  
  130.     if(ss.size() > s.size())
  131.         return;
  132.  
  133.     int HASH = ss[0];
  134.  
  135.     for(int i=1;i<ss.size();i++)
  136.     {
  137.         PP[i] = (PP[i-1] * P)%MOD;
  138.         HASH = (HASH + ss[i]*PP[i])%MOD;
  139.     }
  140.  
  141.     PP[N] = PP[N-1]*P;
  142.     PP[N+1] = PP[N]*P;
  143.  
  144.     HASH = (HASH * PP[N+1])%MOD;
  145.  
  146.     //cout << HASH << "\n";
  147.  
  148.     for(int i=0;i<=s.size()-ss.size();i++)
  149.     {
  150.         if(areEqual(i,i+ss.size()-1,HASH))
  151.             cout << i << " ";
  152.     }
  153.  
  154.     return;
  155. }
  156.  
  157. signed main()
  158. {
  159.     ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
  160.  
  161.     //freopen("input.txt","r",stdin);
  162.     //freopen("output.txt","w",stdout);
  163.  
  164.     int t=1;
  165.     //cin >> t;
  166.  
  167.     while(t--)
  168.     {
  169.         solve_case();
  170.     }
  171.  
  172.     return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement