poj--2752Seek the Name, Seek the Fame KMPnext数组的应用
2015-02-07 17:47
441 查看
题目可以装换成求以最后一个字符结尾的与某一前缀相同的所有后缀的长度;所以需要利用KMP函数中next数组的性质。
我们可以先求出前n-1个字每的next值,然后再求出第n个字母所有可能的(而不是最长的)next值。
当然我们可以先求出所有字母的next值,然后再倒着扫一遍,即next[len],next[next[len]]……..。这样也可以将所有的长度求出来。
注意每一个串的本身是满足一定要求的。
代码如下:
我们可以先求出前n-1个字每的next值,然后再求出第n个字母所有可能的(而不是最长的)next值。
当然我们可以先求出所有字母的next值,然后再倒着扫一遍,即next[len],next[next[len]]……..。这样也可以将所有的长度求出来。
注意每一个串的本身是满足一定要求的。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char str[500000]; int next[500000]; int ans[500000]; int cnt; void Getnext() { int j=0; int n=strlen(str+1); next[1]=0; for(int i=2;i<=n;i++) { if(i==n) { while(j>0) { if(str[j+1]==str[i]) ans[cnt++]=j+1; j=next[j]; } if(str[j+1]==str[i]) ans[cnt++]=j+1; } while(j>0&&str[j+1]!=str[i]) j=next[j]; if(str[j+1]==str[i]) j++; next[i]=j; } } int main() { while(scanf("%s",str+1)!=EOF) { cnt=0; ans[cnt++]=strlen(str+1); Getnext(); for(int i=cnt-1;i>=0;i--) printf("%d ",ans[i]); printf("\n"); } return 0; }
相关文章推荐
- POJ 2752: S 4000 eek the Name, Seek the Fame(简单KMP-NEXT数组的应用)
- POJ-2752 Seek the Name, Seek the Fame(kmp中next数组的应用)
- POJ 2572(Seek the Name, Seek the Fame) KMP中next数组的应用
- POJ - 2752 Seek the Name, Seek the Fame(KMP中对next数组的前缀后缀匹配应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组应用)
- poj2752_Seek the Name,Seek the Fame_KMP(next数组定义的应用)
- POJ-2752-Seek the Name, Seek the Fame(KMPnext数组应用)
- POJ 2752-Seek the Name, Seek the Fame(KMP的next数组运用)
- POJ 2752 Seek the Name, Seek the Fame (kmp next数组运用)
- POJ - 2752 Seek the Name, Seek the Fame (KMP的next[]应用)
- [KMP-NEXT数组性质]POJ 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组的应用)
- POJ2752 Seek the Name, Seek the Fame KMP-next数组的应用
- Seek the Name, Seek the Fame POJ - 2752 (KMP next数组 的 其他用法)
- POJ 2752 Seek the Name, Seek the Fame(next数组的应用)
- POJ2752_Seek the Name, Seek the Fame_KMP_对next数组的运用
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组运用)
- POJ - 2752 Seek the Name, Seek the Fame(KMP next数组的理解)
- POJ 2752 Seek the Name, Seek the Fame(KMP的next数组)