poj2752 Seek the Name,Seek the Fame
2015-12-29 22:47
225 查看
Seek the Name, Seek the Fame
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:)
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.
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
Sample Output
Source
POJ Monthly--2006.01.22,Zeyuan Zhu
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15255 | Accepted: 7720 |
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:)
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.
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
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
Source
POJ Monthly--2006.01.22,Zeyuan Zhu
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<algorithm> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i--) #define ll long long #define pa pair<int,int> #define maxn 400005 #define inf 1000000000 using namespace std; char ch[maxn]; int next[maxn],ans[maxn],cnt,l; inline void getnext() { int i=0,j=-1; next[i]=j; while (i!=l) { if (j==-1||ch[i]==ch[j]) next[++i]=++j; else j=next[j]; } } int main() { while (~scanf("%s",ch)) { l=strlen(ch); getnext(); cnt=0; int tmp=next[l]; while (tmp>0) { ans[++cnt]=tmp; tmp=next[tmp]; } D(i,cnt,1) printf("%d ",ans[i]); printf("%d\n",l); } }
相关文章推荐
- ajax跨域请求
- 设计模式-之-状态模式
- MySQL函数之其他函数
- 自学QT之qss教程
- Android之NDK开发
- poj3630 Phone List
- 字美杯装饮料茶点打印机
- Asp.net MVC 权限过滤器实现方法的最佳实践
- java基于RXTXcomm.jar的串口通信
- JAVA基础04
- div中ul内容逐条上下滚动
- 锤子代工厂倒闭 老罗的“相声”还好笑吗
- ZZULIOJ 1190 查询记录(结构体专题)
- poj2001 Shortest Prefixes
- jquery.validate.js使用之自定义表单验证规则
- [leetcode 311] Sparse Matrix Multiplication
- 锤子代工厂倒闭 老罗的“相声”还好笑吗
- LinkedList
- nodejs的某些api~(二)crypto加密模块
- python中xrange和range的区别