KMP第二题,如果对KMP理解了的话,这道题应该想一下就能想出来
#include<stdio.h>
#include<algorithm>
using namespace std;
char s[400005];
int next[400005];
int a[400005];
//next数组范围为1-n,next[0]是不使用的
void get_next(int n){
if(n==0) return;
int i,j;
j=next[0]=-1;
i=0;
while(i<n){
while(j!=-1 && s[j]!=s[i]) j=next[j];
next[++i]=++j;
}
int len=0;
for(int i=next[n];i!=0;i=next[i]) a[len++]=i;
for(int i=len-1;i>=0;i--) {
printf("%d ",a[i]);
}
//if(next[n]!=0) printf("%d ",next[n]);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int cas=0;
while(scanf("%s",s)!=EOF){
int len=0;
while(s[len]!='\0') len++;
get_next(len);
printf("%d\n",len);
}
return 0;
}POJ 2752 Seek the Name, Seek the Fame
原文地址:http://blog.csdn.net/lj94093/article/details/44700347