Seek the Name, Seek the Fame (POJ_2752) KMP + 最大公共前后缀
来源:互联网 发布:双十一套路 知乎 编辑:程序博客网 时间:2024/06/07 14:09
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15667 Accepted: 7934
Description
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.
Sample Input
ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5
题目大意:给出一串字符串,求出所有公共前后缀。
解题思路:理解Next数组的含义,即最长公共前后缀。
那么找出了整串字符串的最长公共前后缀,如何写出其他的呢?以自身为串继续求最长公共前后缀。
除了调用自身,是否还会存在其他情况,有公共前后缀?不会,因为Next中存的就是最长公共前后缀。----这个很费解。。。
代码如下:
#include"iostream"#include"cstdio"#include"cstring"using namespace std;const int maxn = 400005;char str[maxn];int Next[maxn],ans[maxn];void MakeNext(int m){Next[0] = -1;int i = 0,j = -1;while(i < m){if(j == -1 || str[i] == str[j])Next[++i] = ++j;elsej = Next[j];}}int main(){int len,i;while(scanf("%s",str) != EOF){len = strlen(str);MakeNext(len);i = 0;while(len > 0){ans[++i] = len;len = Next[len];}for(;i >= 1;i --)printf("%d ",ans[i]);printf("\n");}return 0;}
0 0
- Seek the Name, Seek the Fame (POJ_2752) KMP + 最大公共前后缀
- poj_2752 Seek the Name, Seek the Fame(KMP:寻找所有公共前缀后缀)
- poj_2752 Seek the Name, Seek the Fame
- 【kmp算法next数组求解公共前后缀】Seek the Name, Seek the Fame POJ
- POJ2752 Seek the Name, Seek the Fame(前后缀最大匹配,kmp)
- poj2752 Seek the Name, Seek the Fame(KMP前后缀相同)
- POJ 2752 Seek the Name, Seek the Fame [前后缀相同的KMP]
- KMP + 求相等前后缀--- POJ Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame(KMP)
- Seek the Name, Seek the Fame(kmp)
- Seek the Name, Seek the Fame KMP
- kmp Seek the Name, Seek the Fame
- poj2752Seek the Name, Seek the Fame【kmp】
- POJ2752Seek the Name, Seek the Fame (KMP)
- POJ_2752_Seek the Name, Seek the Fame【kmp】
- POJ - 2752 - Seek the Name, Seek the Fame (KMP-打印前缀后缀长度)
- 2752 Seek the Name, Seek the Fame //KMP
- poj 2752 Seek the Name, Seek the Fame(KMP)
- Spring框架的IoC容器详解
- 问题 F: C语言习题 不等长字符串排序
- Java - 给编译器看的Annotation
- Numbers That Count
- C/C++中的函数指针
- Seek the Name, Seek the Fame (POJ_2752) KMP + 最大公共前后缀
- Java集合面试题
- 程序员必须知道的10大基础实用算法及其讲解
- 问题 G: C语言习题 输出月份
- POJ 3067 Japan (树状数组)
- 5. Longest Palindromic Substring | Java最短代码实现
- JSP中page和pageContext的区别
- Jfinal 实现从数据库中提取图片
- CF_1B_Spreadsheets