Seek the Name, Seek the Fame
题目链接:
http://poj.org/problem?id=2752
解题思路:
给你一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度。
利用next数组的存储的性质,即可得出正确的答案。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int N = 400005;
int Next[N],ans[N];
char s[N];
void getnext(){
int j = 0,k = -1;
int l = strlen(s);
Next[0] = -1;
while(j < l){
if(k == -1 || s[j] == s[k]){
j++;k++;
Next[j] = k;
}
else
k = Next[k];
}
}
void kmp(){
getnext();
int l = strlen(s);
int i = l,sum = 0;
while(i != 0){
ans[sum++] = Next[i];
i = Next[i];
}
for(i = sum-2; i >= 0; i--)
printf("%d ",ans[i]);
printf("%d\n",l);
}
int main(){
while(~scanf("%s",s)){
kmp();
}
return 0;
}