Seek the Name, Seek the Fame
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10204   Accepted: 4921

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

题意:

给你一个字符串。要你找出既是前缀又是后缀的子串长度的可能值。按字典序输出。

思路:

转化下思路其实很简单。既然既是前缀又是后缀。直接用文本串构造一个失配数组。然后直接匹配文本串的文本末。即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需转换下思想)的更多相关文章

  1. (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 ...

  2. 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的简单应 ...

  3. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  4. 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 ...

  5. 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 ...

  6. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

    Seek the Name, Seek the Fame Time Limit: 2000MS        Memory Limit: 65536K Total Submissions: 24000 ...

  7. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  8. 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 ...

  9. POJ 2752:Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...

随机推荐

  1. 如何在windows server 2012 R2 部署WEB项目

    tip: 今天发布项目到windows server 2012 R2上面. 没有接触过,其实很简单,看图: 这是安装IIS成功后显示的总图: 二.点击Manage ,选择Add Roles and F ...

  2. SQL Server索引进阶:第五级,包含列

    原文地址: Stairway to SQL Server Indexes: Level 5, Included Columns 本文是SQL Server索引进阶系列(Stairway to SQL ...

  3. hud 2089 不要62 (数位dp)

    #include<stdio.h> #include<string.h> #include<math.h> #define max 10 ]; int number ...

  4. beanUtils操作bean的属性

    beanUtils操纵bean属性: 需要jar包commons-beanutils-x.x.x.jar    同时commons-beanutils-x.x.x.jar需要commons-loggi ...

  5. php学习笔记(3)

    1.计数器 <?php /* simple access counter for php3 (c)1998 David W. Bettis dbettis@eyeintegrated.com m ...

  6. LintCode-编辑距离

    题目描述: 给出两个单词word1和word2,计算出将word1 转换为word2的最少操作次数. 你总共三种操作方法: 插入一个字符 删除一个字符 替换一个字符 样例 给出 work1=" ...

  7. bootstrap读书笔记

    引入bootstrap.js或单个插件的js文件 若引入单个插件的js文件,注意插件之间的依赖关系 data属性api data属性的api很方便,但我们也可以选择关闭这个功能:$(document) ...

  8. 如何给变量取个简短且无歧义的名字(转) good

    湾区日报上分享的一篇文章,文章的作者在Google设计Dart语言,就变量命名方面给了4点建议,文中也列出了好变量名.坏变量名的对比.不管作者的看法与你实际中的命名习惯是否一致,看完这篇文章,相信可以 ...

  9. C++_template_栈的链式存储及实现

    由于在C++数据结构中的代码不完整,特补全.等日后当工程库调用. 若有疑问,请留言. #include<iostream> using namespace std; template< ...

  10. vs linq to db template

    linq to db template 支持sqlite. mysql .db2. accress. oracle. Firebird等多种数据库以linq方式操作数据. NuGet NuGet 是 ...