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. JS - 兼容的事件助手

    (function () { // 兼容的事件助手 window.CompatibleEventHelper = { addEventListener: function (elem, type, c ...

  2. Tinyhttpd 知识点

    1. fork 子进程 #include <stdio.h> #include <unistd.h> int main(void) { pid_t pid; ; pid = f ...

  3. ethereum(以太坊)(五)--Bool

    pragma solidity ^0.4.0; contract Bool{ uint num1 = 100; uint num2 = 200; bool _c = true; // &&am ...

  4. 平衡二叉查找树 AVL 的实现

    不同结构的二叉查找树,查找效率有很大的不同(单支树结构的查找效率退化成了顺序查找).如何解决这个问题呢?关键在于如何最大限度的减小树的深度.正是基于这个想法,平衡二叉树出现了. 平衡二叉树的定义 (A ...

  5. 【转载】char*,const char*和string 三者转换

    本文转自 http://blog.csdn.net/perfumekristy/article/details/7027678 const char* 和string 转换 const char*转换 ...

  6. Python中的str

    str_lst = [ ('元素替换',), ('字符串切片',), ('字符串分割',), ('字符串连接',), ('元素计数',), ('寻找元素',), ('判断字符串的开头与结尾',), ( ...

  7. emplace_back

    c++11 的 list deque 和 vector 增加了emplace_back函数,相对于push_back函数,它减少了一次类的构造,因此效率更高,推荐使用. #include <li ...

  8. [BZOJ1009][HNOI2008]GT考试(KMP+DP)

    [不稳定的传送门 Solution dp[i][j]表示前i个字符当前匹配到不吉利串的第j个,即当前方案的后缀等于不吉利串前缀 然而由于n过大,不能直接转移,用矩阵优化 Code #include & ...

  9. python-12正则表达式

    import re #re.search方法 re.search 扫描整个字符串并返回第一个成功的匹配. re.match('com', 'www.runoob.com') #匹配失败 None re ...

  10. Android Config通用类来记录信息

    1.整体分析 1.1.源代码,可以直接Copy. public class Config { private static int M = 1024 * 1024; private volatile ...