Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define MAXN 2000001
- using namespace std;
- int fail[MAXN];
- void failFunc(string &p){
- int n = p.size();
- fail[0] = -1;
- for(int i = 0; i < n; i++){
- int node = fail[i];
- if(node >= 0 && p[node] != p[i]){
- node = fail[node];
- }
- fail[i+1] = node+1;
- if(p[i] == p[fail[i]]){
- fail[i] = fail[fail[i]];
- }
- }
- }
- void match(string &t, string &p){
- int i = 0, j = 0;
- while(i < (int) t.size()){
- while(j >= 0 && p[j] != t[i]){
- j = fail[j];
- }
- i++; j++;
- if(j == (int) p.size()){
- cout << i - p.size() << endl;
- }
- }
- }
- int n;
- string p,t;
- int main(){
- bool can = 0;
- while(cin >> n){
- if(can) cout << endl;
- can = 1;
- cin >> p >> t;
- failFunc(p);
- match(t,p);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement