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 给一个串s,使它的长度为k的前缀和后缀相同,输出所有k
还是KMP……不过有点坑……原来想倒着做结果发现不对……最后灵机一动才想出做法
先求出s的next数组,然后j=n while (j){ans[++len]=j j=next[j]}这样就行了
这个还是要会到next的定义上去。
next[i]是当前这个匹配不成功的时候可以往前跳到的最长的状态。一开始j=n,然后每次求出一个j,那么j都是n往前跳到的某一个合法状态。
#include<cstdio>
#include<cstring>
char s[1000010];
int next[1000010];
int l,j;
int ans[1000010],len;
int main()
{
while (~scanf("%s",s+1))
{
l=strlen(s+1);j=0;
memset(next,0,sizeof(next));
for (int i=2;i<=l;i++)
{
while (j>0 && s[j+1]!=s[i])j=next[j];
if (s[j+1]==s[i])j++;
next[i]=j;
}
j=l;len=0;
while (j!=0)
{
ans[++len]=j;
j=next[j];
}
for (int i=len;i;i--)printf("%d ",ans[i]);
printf("\n");
}
return 0;
}

poj2752 Seek the Name, Seek the Fame的更多相关文章

  1. POJ2752 Seek the Name, Seek the Fame —— KMP next数组

    题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Li ...

  2. poj-------------(2752)Seek the Name, Seek the Fame(kmp)

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

  3. poj2752 Seek the Name, Seek the Fame(next数组的运用)

    题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...

  4. POJ2752 Seek the Name, Seek the Fame 【KMP】

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

  5. Seek the Name, Seek the Fame (poj2752

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

  6. [poj2752]Seek the Name, Seek the Fame_KMP

    Seek the Name, Seek the Fame poj-2752 题目大意:给出一个字符串p,求所有既是p的前缀又是p的后缀的所有字符串长度,由小到大输出. 注释:$1\le strlen( ...

  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: 14106   Ac ...

  9. Seek the Name, Seek the Fame(Kmp)

    Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (J ...

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

随机推荐

  1. 11636 - Hello World! (贪心法)

    Problem A Hello World!  Input: Standard Input Output: Standard Output When you first made the comput ...

  2. Win7_x64下卸载Oracle11g

    Windows下Oracle11g卸载顺序1.计算机管理-->服务和应用程序-->停止所有Oracle相关服务2.运行D:\app\Administrator\product\11.2.0 ...

  3. 自定义View-6 状态按钮 滑动 点击

    View public class SwitchButton extends View implements OnClickListener, OnTouchListener {     privat ...

  4. 解决:用PivotGridControl 与 chartControl 配合使用,Series最大只显示10条

    修改 PivotGridControl  控件的 OptionsChartDataSource.MaxAllowedSeriesCount 的值就可以了  默认为10条

  5. C# 计算日期时间的间隔天数

    DateTime oldDate = ,,); DateTime newDate = DateTime.Now; // Difference in days, hours, and minutes. ...

  6. Redhat修改本地yum源

    1.将Centos系统的ios文件传到服务器,比如传到/root目录下: 2.将ios文件挂载到本地,需要在本地建立一个文件夹,比如/yum; mkdir /yum mount -o loop /ro ...

  7. 去除input[type=number]最右边的spinners(默认加减符号)

    // 去掉input[type=number]默认的加减号 input[type='number'] { -moz-appearance:textfield; } input[type=number] ...

  8. poj3468 线段树+lazy标记

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92921   ...

  9. discuz3.2x增加邮箱验证功能

    为防止垃圾用户多次注册,为disczu增加邮箱验证功能. 大致分为二步: 1.申请邮箱,这里推荐使用腾讯免费企业邮箱:https://exmail.qq.com/portal/introducefre ...

  10. php echo字符串的连接格式

    echo "<td align=\"center\"><img src=\""; 1.  \"    \" 2. ...