您的位置:首页 > 其它

poj2752 Seek the Name, Seek the Fame(kmp)

2017-07-28 22:26 351 查看
从len开始,每一个fail[len]都可以。。不过要倒着输出。。

#include <cstdio>
#include <cstring>
#define N 400010
char s
;
int fail
,ans
;
inline void getfail(char *t,int m){
int k=0;fail[1]=0;
for(int i=2;i<=m;++i){
while(k&&t[k+1]!=t[i]) k=fail[k];
if(t[k+1]==t[i]) ++k;
fail[i]=k;
}
}
int main(){
//  freopen("a.in","r",stdin);
while(~scanf("%s",s+1)){
int len=strlen(s+1);int tot=0;
getfail(s,len);int k=len;
while(k) ans[++tot]=k,k=fail[k];
for(int i=tot;i>=1;--i) printf("%d ",ans[i]);puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: