Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stack>
- #include <math.h>
- #include <time.h>
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <set>
- #include <iomanip>
- #include <vector>
- #include <map>
- #include <cassert>
- #include <queue>
- #include <tuple>
- using namespace std;
- typedef long long li;
- typedef long double ld;
- #define forn(i, n) for (int i = 0; i < int(n); ++i)
- #define pb push_back
- #define mp make_pair
- #define shek_shek _DEBUG
- #define all(v) v.begin(),v.end()
- #define EPS 1e-9
- #define PI 3.1415926535897932384626433832795
- const int N = 111111;
- const li A = 31;
- const li MOD = 99991;
- int main () {
- #ifdef shek_shek
- freopen("input.txt", " r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- string p, t;
- cin >> p >> t;
- li p_hash = 0, t_hash = 0, p_pow = 1;
- if (p.size() > t.size())
- return 0;
- for (int i = 0; i < p.size(); i++) {
- p_hash = p_hash * A + (p[i] - 'a' + 1);
- t_hash = t_hash * A + (t[i] - 'a' + 1);
- if (i > 0)
- p_pow *= A;
- //p_hash %= MOD;
- //t_hash %= MOD;
- }
- vector <int> ans;
- forn (i, t.size() - p.size() + 1) {
- if (p_hash == t_hash)
- ans.push_back(i + 1);
- t_hash -= p_pow * (t[i] - 'a' + 1);
- t_hash = t_hash * A + (t[i + p.size()] - 'a' + 1);
- //t_hash %= MOD;
- }
- forn(i, ans.size())
- printf("%d ", ans[i]);
- return 0;
- }
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement