题目链接:

id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-size:18px; line-height:26px">http://poj.org/problem?id=2752

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

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

题意:寻找前子串与后子串相等的子串。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 1000017
int next[MAXN];
int ans[MAXN];
int len;
void getnext( char T[])
{
int i = 0, j = -1;
next[0] = -1;
while(i < len)
{
if(j == -1 || T[i] == T[j])
{
i++,j++;
next[i] = j;
}
else
j = next[j];
}
} int main()
{
char ss[MAXN];
int length;
while(~scanf("%s",ss))
{
len = strlen(ss);
getnext(ss);
int n = 0 ;int i = len;
ans[0]=len;
while(next[i] > 0 )//倒着扫描next数组
{//递归查找前子串和后子串相等的子串
i = next[i];
ans[++n] = i ;
}
for( i = n ; i >= 0 ; i--)
printf("%d ",ans[i]);
printf("\n");
}
return 0;
}

poj2752 Seek the Name, Seek the Fame(next数组的运用)的更多相关文章

  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 【KMP】

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

  4. Seek the Name, Seek the Fame (poj2752

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

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

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

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

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

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

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

  9. 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. go (break goto continue)

    package main import ( "fmt" ) func main() { LABEL1: for { ; i < ; i++ { { break LABEL1 ...

  2. delphi 文件的读取(二进制文件和文本文件)

    http://blog.csdn.net/earbao/article/details/9174033

  3. 欧拉计划&#183;第四题

    题目4:找出由两个三位数乘积构成的回文. 一个回文数指的是从左向右和从右向左读都一样的数字.最大的由两个两位数乘积构成的回文数是9009 = 91 * 99. 找出最大的有由个三位数乘积构成的回文数. ...

  4. C++学习笔记10-面向对象

    1.  面向对象的程序设计是基于三个基本概念:数据抽象.继承和动态绑定. 在C++ 在,凭借一流的数据抽象,随着一类从一个类派生还继承:派生类的成员继承基类.决定是使用基类中定义的函数还是派生类中定义 ...

  5. 激动啊,终于诞生了,编译了属于俺自己的 JDK

    激动啊,终于诞生了,编译了属于俺自己的 JDK  折腾了2天,现在编译过去了 30多分钟了,久违的 java.exe 终于出现在了 bin 目录中,属于俺自己的 JDK 终于诞生了,激动啊

  6. hdu1532(最大流)

    传送门:Drainage Ditches 题意:给出n个河流,m个点,以及每个河流的流量,求从1到m点的最大流量. 分析:网络流入门题,第一次写按照白书上毫无优化的Ford_fulkerson算法,先 ...

  7. Android菜鸟的成长笔记(27)——ViewPager的使用

    ViewPager是Android 3.0以上能够使用的API. 一.ViewPager能干什么? 1.微信5.0中连带滑动用ViewPager能够轻松实现. 2.实现相似于新浪微博的导航引导界面. ...

  8. (1)cocos2d-x-2.2.4搭建windows开发环境

    Cocos2d-x-2.2.4搭建windows环境 软件需求 Windows系统(windows7或之后的系统): cocos2d-x-2.2.4压缩包. python安装包(推荐使用2.7.3版本 ...

  9. 【剑指offer】约瑟夫环问题

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/27957407 题目描写叙述: 每年六一儿童节,JOBDU都会准备一些小礼物去看望孤儿院的小 ...

  10. Factorization Machines 学习笔记(四)学习算法

      近期学习了一种叫做 Factorization Machines(简称 FM)的算法.它可对随意的实值向量进行预測.其主要长处包含: 1) 可用于高度稀疏数据场景:2) 具有线性的计算复杂度.本文 ...