poj 2752
来源:互联网 发布:确保数据完整准确 编辑:程序博客网 时间:2024/06/05 10:04
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 19692 Accepted: 10194
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
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5
题目大意:给你一个串,如果这个串存在一个长度为n的前缀串,和长度为n的后缀串,并且这两个串相等,则输出他们的长度n。求出所有的长度n。
KMP模板
代码:
#include <cstdio>#include <iostream>#include <string>using namespace std;int p[400000];char a[400000];void dfs(int x){ if (p[x]>-1) { dfs(p[x]); cout<<p[x]+1<<' '; } }int main(){ while (gets(a)!=NULL) { int m; m=strlen(a); p[0]=-1; int j=-1; for (int i=1;i<=m-1;i++) { while (j>-1&&a[j+1]!=a[i]) j=p[j]; if (a[j+1]==a[i]) j++; p[i]=j; } dfs(m-1); cout<<m<<endl; }}
- POJ 2752
- poj 2752
- poj 2752
- POJ 2752
- poj 2752
- POJ 2752
- poj(2752)
- POJ 2752
- poj 2752
- POJ 2752
- poj 2752
- POJ-2752
- POJ 2752
- POJ-2752
- poj 2752
- Poj 2752
- poj 2752
- POJ 2752
- 多文件目录下makefile文件递归执行编译所有c文件
- bit位域大小端转换问题,位域是一个整体转换?
- Opencv实现粒子滤波算法
- 【Linux】 nc (netcat) 详解
- The APK file xxx.apk does not exist on disk. Error while Installing APK
- poj 2752
- Android学习路线总结,绝对干货
- 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
- python 获得对象的所有属性和方法
- 51定时器初始化步骤
- 简单几步实现在AndroidStudio中导出Jar包
- libcore.java.text.DateFormatSymbolsTest测试fail两条
- HDU 3501 数论 欧拉函数的应用
- ServletContext域对象