关于MP(非KMP)算法中出现的mpnext数组的应用
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxs= 4e5+3;
char s[maxs];
int nxt[maxs];
int ans[maxs];
void PreKmp(int l)
{
int i= 0, j;
j= nxt[0]= -1;
while (i< l){
while (j> -1 && s[i]!= s[j]){
j= nxt[j];
}
nxt[++i]= ++j;
}
}
int main()
{
while (EOF!= scanf(" %s", s)){
int ls= strlen(s);
PreKmp(ls);
int la= 0, j= ls;
while (j> 0){
ans[la++]= j;
j= nxt[j];
}
while (la--){
printf("%d ", ans[la]);
}
putchar('
');
}
return 0;
}