Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define speed ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  4. #define sz(x) (int)(x.size())
  5. typedef long long ll;
  6. const int N = 2e5+5;
  7. const int mod = 1e9+7;
  8. const int P = 31;
  9. string s, t;
  10. int h[N], p[N], hasht;
  11. int mul(int a, int b){
  12.     return a*1LL*b % mod;
  13. }
  14. int get_hash(int l, int r){
  15.     return (h[r] - h[l-1]*p[r-l+1] + mod*1LL*mod) % mod;
  16. }
  17. void solve(){
  18.     cin >> s >> t;
  19.     p[0] = 1;
  20.     for(int i = 1; i <= 2e5; i++)
  21.         p[i] = mul(p[i-1], P);
  22.     for(int i = 1; i <= sz(s); i++){
  23.         h[i] = (h[i-1] + (s[i-1] - 'a' + 1) * p[i-1]) % mod;
  24.     }
  25.     for(int i = 1; i <= sz(t); i++){
  26.         hasht += ((t[i-1] - 'a' + 1) * p[i-1]);
  27.         hasht %= mod;
  28.     }
  29.     for(int i = sz(t); i <= sz(s); i++){
  30.         int l = i-sz(t)+1, r = i;;
  31.         if(get_hash(l, r) == hasht)
  32.             cout << l-1 << ' ';
  33.     }
  34. }
  35. signed main(){
  36.     speed;
  37.     solve();
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement