-
Notifications
You must be signed in to change notification settings - Fork 1
/
KMP
37 lines (34 loc) · 764 Bytes
/
KMP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <vector>
#include <iostream>
#include <cstdio>
#define M 2000009
using namespace std;
char S[M],T[M];
int fail[M];
vector<int> ans;
int main(){
int i;
cin.getline(T,M);
cin.getline(S,M);
int now=-1;
fail[0]=now;
for (i=1;S[i];i++){
while(now>=0 && S[now+1]!=S[i]) now=fail[now];
if (S[now+1]==S[i]) now++;
fail[i]=now;
}
int len = i;
now = -1;
for (i=0;T[i];i++){
while(now>=0 && S[now+1]!=T[i]) now=fail[now];
if (S[now+1]==T[i]) now++;
if (!S[now+1]){
ans.push_back(i-len+2);
now=fail[now];
}
}
printf("%d\n",(int)ans.size());
for (i=0;i<ans.size();i++) printf("%d ",ans[i]);
return 0;
}