博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2752 Seek the Name, Seek the Fame KMP
阅读量:7313 次
发布时间:2019-06-30

本文共 2252 字,大约阅读时间需要 7 分钟。

Language: Default
Seek the Name, Seek the Fame
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 12466   Accepted: 6139

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

ababcababababcababaaaaa

Sample Output

2 4 9 181 2 3 4 5

Source

,Zeyuan Zhu

题意:给一个字符串str,求出既是前缀又是后缀的所有长度。这一题是KMP的next数组的应用。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 400010#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int next[maxn],ans[maxn];char str[maxn];void get_next(){ int len=strlen(str); int i=0,j=-1; next[0]=-1; while (i
0) //递推出所有的长度并记录在ans数组里 { ans[t]=next[j]; t++; j=next[j]; } for (int i=t-1;i>0;i--) printf("%d ",ans[i]); printf("%d\n",ans[0]);}int main(){ while (scanf("%s",str)!=EOF) get_next(); return 0;}/*ababcababababcababaaaaa*/

转载于:https://www.cnblogs.com/i8888/p/4044001.html

你可能感兴趣的文章
手机拒接电话可完善之处
查看>>
每日编程系列——优雅的点
查看>>
T-SQL语句查看作业等信息
查看>>
1083. List Grades (25)
查看>>
Codechef April Challenge 2019 游记
查看>>
陶哲轩实分析习题17.1.2
查看>>
《几何与代数导引》习题1.35.5
查看>>
陶哲轩实分析 习题 7.2.6 (嵌套级数)
查看>>
.net手动编写Windows服务
查看>>
Python文摘:Mixin 2
查看>>
JBoss的配置
查看>>
对XML的收集1
查看>>
性能测试学习 第一课
查看>>
IIS7 windows 下安装PHP
查看>>
关于ASP.NET中Request.QueryString的乱码问题(转)
查看>>
b继承a的函数
查看>>
风险案例-26期-由于签订合同内容简单, 合作双方对范围没有达成一致认可或承诺,导致验收困难、无法顺利结项...
查看>>
iOS 之加密方式
查看>>
Source Insight 常用设置和快捷键大全
查看>>
剑指OFFER(百度笔试)——二叉树的子结构
查看>>