POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24000 Accepted: 12525
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
题意:给出一个字符串str,求出str中存在多少子串,既是str的前缀,又是str的后缀,从小到大输出长度(开一个数组倒序输出即可)
#include<iostream>
#include<string.h>
using namespace std;
char s[411111];
int pp,nex[411111],nexx[411111];
void getnext()
{
int i=0,k=-1;
nex[0]=-1;
while(i<pp)
{
if(k==-1||s[i]==s[k])
nex[++i]=++k;//,cout<<nex[i];
else
k=nex[k];
}
}
int main()
{
int j;
while(~scanf("%s",s))
{
j=0;
pp=strlen(s);
getnext();
for(int i=pp;nex[i]!=-1;i=nex[i])
nexx[j++]=i;
for(int i=j-1;i>=0;i--)
i?printf("%d ",nexx[i]):printf("%d\n",nexx[i]);
}
return 0;
}
POJ 2752 Seek the Name, Seek the Fame(next数组运用)的更多相关文章
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
- Seek the Name, Seek the Fame POJ - 2752
Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
- POJ 2752:Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...
随机推荐
- on条件与where条件的区别
数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...
- 条理清晰的搭建SSH环境之添加所需jar包
一.首先介绍要添加框架环境: JUnit Struts2 Hibernate Spring (1)配置JUnit /**-------------------------添加JUnit-------- ...
- idea中的常用快捷键
idea中常用的快捷键以及一些奇淫技巧 , 加快我们的开发效率 !!! 知道类名查找你本地以的类 (你pom中依赖的类+你自己创建的类)-------------> ctrl+shi ...
- .NET MVC中的ActionResult
一 摘要 本文介绍了ASP.NET MVC中的ActionResult,本节主要介绍 EmptyResult / Content Result /JavaScriptResult /JsonResu ...
- oracle乱码记录
plsql中显示正常,查询到的结果乱码 1.获得服务端编码 select * from nls_database_parameters where parameter ='NLS_CHARACTERS ...
- DotNetBar滚动条的疑似BUG
1.重现过程,在winform窗体上拖一个VScrollBarAdv 2.Button里点击跟踪代码 3.Value居然是-5,,而不是0,这是直接赋值,不是手动拖的呀. 4.解决办法,将LargeC ...
- u-boot移植(十二)---代码修改---支持DM9000网卡
一.准备工作 1.1 原理图 CONFIG_DM9000_BASE 片选信号是接在nGCS4引脚,若要确定网卡的基地址,则要根据片选信号的接口去确定. 在三星2440的DATASHEET中memory ...
- transform,变换
1.transform属性:rotate(翻转),skew(倾斜),scale(缩放),translate(移位) 用法:transform: rotate(45deg) scale(0.5) ske ...
- GNU/Linux的GNU是什么意思
这个组织中黑客云集,而且多是掌握核心技术的真正高手,他们的作品多是编译器.词法/语法分析器.底层函数库等大作.更重要的不是他们的技术,而是他们的哲学!他们的哲学就是技术上的“共产主义”——人人为我,我 ...
- SpringBoot使用Redis缓存
(1).添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...