【POJ 2752 Seek the Name, Seek the Fame】
Time Limit: 2000MS
Memory Limit: 65536K
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
Source
POJ Monthly--2006.01.22,Zeyuan Zhu
题解:
①题目要求求出一个串的所有和某后缀相等的前缀的长度。
②KMP的NEXT数组递归求解
#include<stdio.h>
#include<cstring>
#define go(i,a,b) for(int i=a;i<=b;i++)
using namespace std;const int N=1000003;
int m,f[N],j,S[N],s;char P[N];
int main()
{
f[0]=f[1]=0;
while(~scanf("%s",P))
{
m=strlen(P);
go(i,1,m-1){j=f[i];while(j&&P[j]!=P[i])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
j=m;while(j)S[++s]=j,j=f[j];while(s)printf("%d ",S[s--]);puts("");
}
return 0;
}//Paul_Guderian
那年我二十五岁,转眼许多年过去我真的成功了
我的歌传遍了大街小巷,我有了一个家和可爱的女儿,
有了一群志同道合的朋友……————————汪峰《那年我五岁》
【POJ 2752 Seek the Name, Seek the Fame】的更多相关文章
- 【POJ 2752】 Seek the Name, Seek the Fame
[题目链接] 点击打开链接 [算法] KMP 沿着失配指针扫一遍即可 [代码] #include <algorithm> #include <bitset> #include ...
- 【poj 2752】Seek the Name, Seek the Fame(字符串--KMP)
题意:给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 解法:利用KMP中next[ ]数组的性质,依次找到前缀.后缀匹 ...
- 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 ...
- (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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
随机推荐
- svn+apache安装配置
1.安装httpd,mod_dav_svn,subversion yum install -y httpd mod_dav_svn subversion 2.创建仓库 mkdir /var/www/s ...
- RedHat6.4安装图形行化界面
1.1 打开电源进入RedHat shell命令行界面 1.2 查看系统镜像包括的所有软件包组信息 [root@zhongyi-test ~]# yum grouplist Loaded ...
- windows和linux上mysql的安装
mysql基于多平台,多版本的安装 mysql.tar.gz 链接:https://pan.baidu.com/s/1lG9BNL1mG4qbjM8xLHtrjQ 密码:s4tk MySQL 是一个 ...
- 微信小程序CheckBox选中事件
1.微信小程CheckBox选中问题 <checkbox-group bindchange="checkboxChange" data-index="{{index ...
- python学习之循环语句
编程语言中的循环语句,以循环判断达式是否成立为条件,若表达式成立则循环执行该表达式,若不成立则跳出当前执行执行语句且继续执行其后代码. 如下图所示. Python中提供以下循环方式 循环类型 描述 w ...
- POJ:1017-Packets(贪心+模拟,神烦)
传送门:http://poj.org/problem?id=1017 Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- Gold Balanced Lineup POJ - 3274
Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been abl ...
- 笔记-scrapy-Request/Response
笔记-scrapy-Request/Response 1. 简介 Scrapy使用Request和Response来爬取网站. 2. request class scrapy.http ...
- WPF开发实例——仿QQ登录界面
原文:WPF开发实例--仿QQ登录界面 版权声明:本文为博主原创文章,如需转载请标明转载地址 http://blog.csdn.net/u013981858 https://blog.csdn.net ...
- C#方法参数
使用静态字段来模拟全局变量. 如果调用者想要得到被调用者的值: 1.返回值 2.不管是实参还是形参,都是在内存中开辟了空间的. 3.方法的功能一定要单一. GetMax(int n1,int n2) ...