http://blog.csdn.net/qq_30974369/article/details/74276186
#include<bits/stdc++.h>
using namespace std;
const int maxc = 1000010;
char a[maxc], b[10010];
int fail[10010];//勿用next!!!那是保留字。fail[i]表示1-i这个子串的最大公共前后缀长度。
int main(){
scanf("%s%s", a+1, b+1);
int n = strlen(a+1);
//cout << n << endl;
int m = strlen(b+1);
//cout << m << endl;
for(int i = 2, j = 0; i <= m; i++){//自匹配求fail,j前i后
while(j && b[i] != b[j+1]) j = fail[j]; //不匹配就找前面的最长公共前后缀长度。
if(b[i] == b[j+1]) j++;
fail[i] = j;
}
for(int i = 1, j = 0; i <= n; i++){
while(j && b[j+1] != a[i]) j = fail[j];
if(b[j+1] == a[i]) j++;
if(j == m) printf("%d\n", i-m+1);
}
for(int i = 1; i <= m; i++)
printf("%d ", fail[i]);
printf("\n");
return 0;
}