Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int64_t long long
- using namespace std;
- const int N = 1005;
- const int P = 311;
- const int BASE = 1000000007LL;
- string s, t;
- int n, m;
- int64_t Pow[N];
- int64_t A[N], B[N];
- void hash_build(string s, int n, int64_t Hash[]) {
- for(int i = 1; i <= n; ++i)
- Hash[i] = (Hash[i-1] * P + s[i-1]) % BASE;
- }
- int64_t hash_range(int L, int R, int64_t Hash[]) {
- return (Hash[R] - Hash[L - 1] * Pow[R - L + 1] + 1LL * BASE * BASE) % BASE;
- }
- int main() {
- freopen("in.txt", "r", stdin);
- //freopen("Hash.inp", "r", stdin);
- //freopen("Hash.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- Pow[0] = 1;
- for(int i = 1; i <= N; ++i)
- Pow[i] = (Pow[i-1] * P) % BASE;
- cin >> s >> t;
- n = s.size();
- m = t.size();
- hash_build(s, n, A);
- hash_build(t, m, B);
- for(int i = 1; i <= n - m + 1; ++i) {
- int l = i, r = i + m - 1;
- if(hash_range(l, r, A) == B[m])
- cout << i << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment