求相等前后缀长度
代码:
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#define sf scanf
#define pf printf
using namespace std;
const int maxn = 400000 + 50;
char str[maxn],fail[maxn];
int getFail(){
int len = strlen(str);
fail[0] = fail[1] = 0;
for(int i = 1;i < len;++i){
int j = fail[i];
while(j && str[j] != str[i]) j = fail[j];
fail[i + 1] = (str[j] == str[i] ? j + 1 : 0);
}
return len;
}
int ans[maxn],cnt;
int main(){
// freopen("data.txt","r",stdin);
while(sf("%s",str) != EOF){
int len = getFail();
int j = fail[len];
cnt = 0;
while(j){
ans[++cnt] = j; j = fail[j];
}
for(int i = cnt;i;--i){
pf("%d ",ans[i]);
}pf("%d\n",len);
}
return 0;
}