POJ-2752(KMP(Seek the Name, Seek the Fame))
来源:互联网 发布:ndcg优化 编辑:程序博客网 时间:2024/06/05 05:39
POJ-2752()(KMP)(Seek the Name, Seek the Fame)
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15833 Accepted: 8039
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
My soution:
/*2016.4.12*/
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char c[400010];int nest[400010],len,cnt[400010];void getnest(){int i=0,j=-1,k;nest[0]=-1;len=strlen(c);while(i<len){if(j==-1||c[i]==c[j]){i++,j++;nest[i]=j;}elsej=nest[j];}return ;}void solve(){int i=1,j,k,y,x;getnest();x=len-1;y=nest[x];while(y!=-1){if(c[x]==c[y])cnt[i++]=y+1;//记录子串长度 y=nest[y];//递归 }cnt[0]=len;//字符串本身也满足前缀、后缀相等 for(j=i-1;j>0;j--)printf("%d ",cnt[j]);printf("%d\n",cnt[0]);}int main(){int i,j;while(scanf("%s",c)!=EOF){ solve();}return 0;}
0 0
- 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
- [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)
- 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)
- 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)
- 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
- 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】
- win7开机后svchost..exe占用CPU和内存很高的解决方法
- 工控系统蜜罐建设与协议仿真技术分享
- 用CornerStone配置SVN,HTTP及svn简单使用说明
- 【awk】文本处理
- 常用正则表达式
- POJ-2752(KMP(Seek the Name, Seek the Fame))
- 【java学习】不同类型的转换
- 【cut】剪断文件
- JS求两个数组 差集
- I O编程
- Linux上SD/MMC卡驱动移植调试记录
- jquery.tmpl.js 详解
- java代码设置布局的宽和高
- CDISC: SDTM Finding datasets Original and Standardized Results conversion