POJ 2752 Seek the Name, Seekthe Fame(KMP:后缀与前缀)
2014-04-06 12:32
423 查看
POJ 2752 Seek the Name, Seekthe Fame(KMP:后缀与前缀)
http://poj.org/problem?id=2752
题意:
给你一个串T,找出串T的子串,该串既是T的前缀也是T的后缀。从小到大输出所有符合要求的串的长度。
分析:
首先要知道KMP的next[i]数组求得的数值就是串T中的[1,i-1]的后缀与串T中的[0,i-2]前缀的最大匹配长度。
所以next[m](m为串长且串从0到m-1下标)的值就是与后缀匹配的最大前缀长度(想想是不是)。
next[next[m]]也是一个与后缀匹配的前缀长度,,,依次类推即可。
AC代码:
http://poj.org/problem?id=2752
题意:
给你一个串T,找出串T的子串,该串既是T的前缀也是T的后缀。从小到大输出所有符合要求的串的长度。
分析:
首先要知道KMP的next[i]数组求得的数值就是串T中的[1,i-1]的后缀与串T中的[0,i-2]前缀的最大匹配长度。
所以next[m](m为串长且串从0到m-1下标)的值就是与后缀匹配的最大前缀长度(想想是不是)。
next[next[m]]也是一个与后缀匹配的前缀长度,,,依次类推即可。
AC代码:
#include <iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<vector> using namespace std; const int MAXN=400000+1000; char P[MAXN]; int m,next[MAXN]; void getFail() { next[0]=next[1]=0; for(int i=1;i<m;i++) { int j=next[i]; while(j && P[i]!=P[j]) j=next[j]; next[i+1] = (P[i]==P[j])?j+1:0; } } int main() { while(scanf("%s",P)==1) { vector<int> vc; m = strlen(P); getFail(); vc.push_back(m); int i=m; while(next[i]) { i=next[i]; vc.push_back(i); } for(int i=vc.size()-1;i>0;i--) printf("%d ",vc[i]); printf("%d\n",vc[0]); } return 0; }
相关文章推荐
- POJ 2752 Seek the Name, Seek the Fame kmp(后缀与前缀)
- 【Kmp求既是前缀又是后缀的子串】POJ - 2752 Seek the Name, Seek the Fame
- poj_2752 Seek the Name, Seek the Fame(KMP:寻找所有公共前缀后缀)
- 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(求所有既是前缀又是后缀的子串长度)
- poj 2752 Seek the Name, Seek the Fame(找出所有前缀等于后缀的位置)
- POJ 2752 Seek the Name, Seek the Fame [前后缀相同的KMP]
- POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame(KMP前后缀相同)
- poj 2752 Seek the Name, Seek the Fame 【在原串中 找所有既是原串前缀又是原串后缀的子串长度】
- POJ2752 Seek the Name, Seek the Fame(前后缀最大匹配,kmp)
- POJ2752 Seek the Name, Seek the Fame 【既是前缀又是后缀的子串】
- (字符串的模式匹配4.7.19——前缀数组suffix的应用)POJ 2752 Seek the Name, Seek the Fame(求解一个字符串中前缀和后缀一样的位置)
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name,Seek the Fame(KMP应用)
- POJ2752[Seek the Name, Seek the Fame] kmp
- POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木
- poj 2752 Seek the Name, Seek the Fame(KMP应用)
- [poj 2752] Seek the Name, Seek the Fame[KMP]