poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame
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. 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 Sample Output 2 4 9 18 Source POJ Monthly--2006.01.22,Zeyuan Zhu
|
题意:
给你一个字符串。要你找出既是前缀又是后缀的子串长度的可能值。按字典序输出。
思路:
转化下思路其实很简单。既然既是前缀又是后缀。直接用文本串构造一个失配数组。然后直接匹配文本串的文本末。即txt[n].n=strlen(n)。而长度就为f[j]。想一想就明白了。感觉思想还是蛮好的。
详细见代码:
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
char txt[400100];
int f[400100],ans[400100],cnt;
void getf(char *p)//得到失配数组
{
int i,j,m=strlen(p);
f[0]=f[1]=0;
for(i=1;i<m;i++)
{
j=f[i];
while(j&&p[j]!=p[i])
j=f[j];
f[i+1]=p[j]==p[i]?j+1:0;
}
}
int main()
{
int len,i,j; while(~scanf("%s",txt))
{
getf(txt);
len=strlen(txt);
cnt=0;
j=len;
while(f[j])
{
ans[cnt++]=f[j];
j=f[j];
}
for(i=cnt-1;i>=0;i--)
printf("%d ",ans[i]);
printf("%d\n",len);//注意自己是自己的前后缀
}
return 0;
}
poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)的更多相关文章
- (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: 14106 Ac ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- 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 ...
随机推荐
- ASP.NET -- repeater控件的使用
转载自网络-原网址[http://blog.csdn.net/haitaodoit/article/details/7021214] repeater绑定数据: protected void Page ...
- windows下fitness python版本安装测试
FitNesse介绍¶ FitNesse是一套软件开发协作工具. 伟大的软件需要协作和交流,FitNesse可以帮助大家加强软件开发过程中的协作.能够让客户.测试人员和开发人员了解软件要做成什么样,自 ...
- codeforces 616E. Sum of Remainders 数学
题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...
- python函数abs()
详解: 返回绝对值 参数可以是:负数.正数.浮点数或者长整形 实例: abs(-1.2) #返回 1.2 abs(1.2) #返回 1.2 abs(-11216.5) #返回 11216.5 abs( ...
- c 结构体 简单的了解
1.声明一个学生类的 结构体 struct Student{ int age; char name[20];//长度为20的字符串 int weiht;//像正常一样的申请变量,这个变量属于结构体的一 ...
- 使用CarrierWave上传图片时,多版本文件名的统一
第一次使用CarrierWavewe做上传,不能不说,虽然Rails已经把上传变得超简单了,而CarrierWave则是把上传变成了一种享受,特别是做图片上传,现在这年代,图片展示平台已经不仅仅是电脑 ...
- 从事web前端两年半后的迷茫
做了两年半的重构,突然有种迷茫的感觉,好像瓶颈了,不知道自己该怎么继续走下去,以前刚毕业的时候,总觉得自己有好多的东西要学在前端方面,所以有那个促使自己去学习的动力,每当没工作任务的时候,自己总是去主 ...
- 宣布发布 Windows Azure SDK 2.2,正式发布 Windows Azure Backup 和 Hyper-V 恢复管理器预览版
开发人员正逐渐转向云计算,因为它具有众多优势,包括成本.自动化和让开发人员专注于应用程序逻辑的能力.我们很高兴地宣布,继推出 Visual Studio 2013 之后,今天将发布 Windows A ...
- [转]WIBKIT技术资料
WebKit结构和流程分析 http://inedx.blog.hexun.com/28830354_d.html webkit架构 http://inedx.blog.hexun.com/28795 ...
- WebKit学习要点
<体系结构详解> 1. 浏览器功能结构 2. 浏览器结构 3. Webkit体系结构 4. WebKit目录结构 5. WebKit编译 <HTML引擎详解> 1. HTML语 ...