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】的更多相关文章

  1. 【POJ 2752】 Seek the Name, Seek the Fame

    [题目链接] 点击打开链接 [算法] KMP 沿着失配指针扫一遍即可 [代码] #include <algorithm> #include <bitset> #include ...

  2. 【poj 2752】Seek the Name, Seek the Fame(字符串--KMP)

    题意:给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 解法:利用KMP中next[ ]数组的性质,依次找到前缀.后缀匹 ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. Centos 7 下安装mysql

    // 卸载原有maridb-lib库 [root@localhost ~]# rpm -qa | grep mariadb mariadb-libs-5.5.41-2.el7_0.x86_64 [ro ...

  2. 图解HTTP总结(1)——了解Web及网络基础

    Web页面不能凭空显示出来.根据Web浏览器地址栏指定的URL,Web浏览器从Web服务器端获取文件资源等信息,从而显示出Web页面. Web使用一种名为HTTP(HyperText  Transfe ...

  3. Visual Stutio 2015激活密钥

    Visual Stutio 2015 专业版激活密钥:HMGNV-WCYXV-X7G9W-YCX63-B98R2 Visual Stutio 2015 企业版激活密钥:HM6NR-QXX7C-DFW2 ...

  4. WRITE命令 書式設定オプション

    書式設定オプション WRITE 命令では.さまざまな書式設定オプションが使用することができます. 構文 WRITE ....f option. 全データ型の書式設定オプション オプション 機能 LEF ...

  5. 适配IE8+等浏览器的适配播放插件

    function myBrowser(){ var userAgent = navigator.userAgent; //ȡ���������userAgent�ַ� var isOpera = us ...

  6. Android 热点相关操作

    Android未提供对该API的直接访问, 需要使用反射, 代码较简单, 如下 GetHotspotState.java package club.seliote.hotspotscanner.uti ...

  7. 大话CNN经典模型:AlexNet

    2012年,Alex Krizhevsky.Ilya Sutskever在多伦多大学Geoff Hinton的实验室设计出了一个深层的卷积神经网络AlexNet,夺得了2012年ImageNet LS ...

  8. UnicodeDecodeError: 'gbk' codec can't decode byte 0xab in position 11126: illegal multibyte sequence

    python读取文件中含有中文时, 会报错: 解决办法是:打开文件时以utf-8格式打开,同样适用于gbk

  9. 20145202马超《java程序设计》第一周学习总结

    这两天的学习让我对java有了初步的了解. 1.java是SUN公司推出的面相网络的编程语言. 特点:完全面向对象,与平台无关,跨平台性(例如c++只能在windows上执行,然而java并没有这些限 ...

  10. 2 js的20/80关键知识

    1. 2 var a = 1; undefined a 1 alert(a); undefined var b = true; var c = "Hi"; undefined al ...