http://poj.org/problem?id=2752

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

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

  1. ababcababababcabab
  2. aaaaa

Sample Output

  1. 2 4 9 18
  2. 1 2 3 4 5
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<stack>
  5. using namespace std;
  6.  
  7. #define N 1000007
  8. #define max(a,b) (a>b?a:b)
  9.  
  10. int Next[N];
  11. char S[N];
  12.  
  13. void FindNext(int Slen)
  14. {
  15. int i=, j=-;
  16. Next[] = -;
  17.  
  18. while(i<Slen)
  19. {
  20. if(j==- || S[i]==S[j])
  21. Next[++i] = ++j;
  22. else
  23. j = Next[j];
  24. }
  25. }
  26.  
  27. int main()
  28. {
  29.  
  30. while(scanf("%s", S)!=EOF)
  31. {
  32. int Slen, n;
  33.  
  34. Slen = strlen(S);
  35. n = Slen;
  36. FindNext(Slen);
  37.  
  38. stack<int>Q;
  39.  
  40. while(n)
  41. {
  42. Q.push(Next[n]);
  43. n = Next[n];
  44. }
  45.  
  46. Q.pop();
  47. while(Q.size())
  48. {
  49. printf("%d ", Q.top());
  50. Q.pop();
  51. }
  52.  
  53. printf("%d\n", Slen);
  54. }
  55. return ;
  56. }

(KMP)Seek the Name, Seek the Fame -- poj --2752的更多相关文章

  1. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  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. Seek the Name, Seek the Fame(Kmp)

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

  4. Seek the Name, Seek the Fame POJ - 2752(拓展kmp || kmp)

    题意: 就是求前缀和后缀相同的那个子串的长度  然后从小到大输出 解析: emm...网上都用kmp...我..用拓展kmp做的  这就是拓展kmp板题嘛... 求出extend数组后  把exten ...

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

  6. (转)python文件操作 seek(),tell()

    seek():移动文件读取指针到指定位置 tell():返回文件读取指针的位置 seek()的三种模式: (1)f.seek(p,0)  移动当文件第p个字节处,绝对位置 (2)f.seek(p,1) ...

  7. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  8. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  9. codeM编程大赛E题 (暴力+字符串匹配(kmp))

    题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...

随机推荐

  1. Axel与Wget下载工具

    Axel工具是linux下的http/ftp中强大下载工具,支持多线程和断点续传下载.且可以从多个地址或者从一个地址的多个连接来下载同一个文件. 常用的选项: [root@wjoyxt ~]# axe ...

  2. ResponseUtil数据传递至前端

    package com.java1234.util; import java.io.OutputStream; import java.io.PrintWriter; import javax.ser ...

  3. Unity 所有特殊文件夹

    1.Editor 2.Editor Default Resources Editor Default Resources注意中间是有空格的,它必须放在Project视图的根目录下,如果你想放在/xxx ...

  4. oracle 查看被锁的表和解锁

    相关视图 SELECT * FROM v$lock;SELECT * FROM v$sqlarea;SELECT * FROM v$session;SELECT * FROM v$process ;S ...

  5. java script btoa与atob的

    javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...

  6. Spring Boot logback

    前言 今天来介绍下spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  7. Java8 Stream语法详解 2

    1. Stream初体验 我们先来看看Java里面是怎么定义Stream的: A sequence of elements supporting sequential and parallel agg ...

  8. CentOS7.0重置Root的密码 (忘记密码)

    首先进入开启菜单,按下e键进入编辑现有的内核,如下图所示 然后滚动列表,找到ro,将它替换成rw,并加上init=/sysroot/bin/sh,最终变为如下图 然后按CTRL+X进入到单用户模式,在 ...

  9. hdoj1069 Monkey and Banana(DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 思路: 由题意,显然一种block可能有6种形式,且一种形式最多使用一次,因此最多有30×6=1 ...

  10. nyoj86-找球号(一) 【set 二分查找 hash】

    http://acm.nyist.net/JudgeOnline/problem.php?pid=86 找球号(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ...