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 ...
随机推荐
- 搭建ftp
相信很多人都知道ftp吧.打个比方.在你们公司或者学校里面有一个ftp地址,里面存放了你们所需要的一些常用的资源.这样的话你们就可以随时登录这个ftp来拉取你所需要的资源(在范围内),简单的来说,ft ...
- 2、Python-流程控制
比较运算符 运算符 描述 示例 == 检查两个操作数的值是否相等,如果是则条件变为真. 如a=3,b=3则(a == b) 为 true. != 检查两个操作数的值是否相等,如果值不相等,则条件变为真 ...
- impala记录-安装kudu和impala
1.配置/etc/yum.repos.d clouder-kudu.repo [cloudera-kudu]# Packages for Cloudera's Distribution for kud ...
- POJ - 1426 Find The Multiple(搜索+数论)
转载自:優YoU http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...
- 20155332 2016-2017-2 《Java程序设计》第8周学习总结
20155332 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 创建Logger对象 static Logger getLogger(String name ...
- SQL Server 2008“备份集中的数据库备份与现有的数据库不同”解决方法
对于SQL Server 2008,有几个地方是要注意的,比方在还原数据库时,不像2000里边将数据库和文件区分的很细, 统一均为文件,这就使还原的数据库文件制定为. bak.那么想还原2000的数据 ...
- chrome面板介绍
Chrome开发者工具详解(1):Elements.Console.Sources面板 Chrome 开发者工具详解(2):Network 面板 Chrome开发者工具详解(3):Timeline面板 ...
- Caffe源码阅读(1) 全连接层
Caffe源码阅读(1) 全连接层 发表于 2014-09-15 | 今天看全连接层的实现.主要看的是https://github.com/BVLC/caffe/blob/master/src ...
- android彻底关闭应用程序方法
Android SDK > 7(Android2.1)之后,即Android2.2及以后版本彻底关闭应用的方法,目前试验只有一下方法有效: Intent startMain = new Inte ...
- xargs -i和-I的区别【转】
xargs与find经常结合来进行文件操作,平时删日志的时候只是习惯的去删除,比如 # find . -type f -name "*.log" | xargs rm -rf * ...