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 ...
随机推荐
- HTML静态网页(框架)
1.frameset frameset最外层,使用时需要去除body改用frameset. <frameset rows="100,*" frameborder=" ...
- IS-A 和 HAS-A
IS-A关系 IS-A就是说:一个对象是另一个对象的一个分类. 下面是使用关键字extends实现继承. public class Animal{ } public class Mammal exte ...
- C++类包含问题(重复包含和相互包含)
一.重复包含头文件 头文件重复包含,可能会导致的错误包括:变量重定义,类型重定义及其他一些莫名其妙的错误.C++提供两种解决方案,分别是#ifndef和#pragma once #ifndef _SO ...
- [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...
- BZOJ 1816: [Cqoi2010]扑克牌( 二分答案 )
二分答案.. 一开始二分的初始右边界太小了然后WA,最后一气之下把它改成了INF... -------------------------------------------------------- ...
- 2768: [JLOI2010]冠军调查( 最小割 )
最小割... 怎么乱搞都可以 -------------------------------------------------------------------------------- #inc ...
- Hibernate学习之缓存机制
转自:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html 一.N+1问题 首先我们来探讨一下N+1的问题,我们先通过一个例子来看一下,什么是N ...
- 利用python进行数据分析之pandas库的应用(一)
一.pandas的数据结构介绍 Series Series是由一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据索引构成.仅由一组数据可产生最简单的Series. obj=Series([4 ...
- android-JSON解析
构建JSON文本 方法1. // 假设现在要创建这样一个json文本 // { // "phone" : ["12345678", "87654321 ...