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

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

题意:求一个串中满足前缀等于后缀的子串的长度
这里要跟回文串区别开来,回文是从前往后和从后往前完全匹配;
而这个是前面一部分与后面一部分完全匹配。
题解:kmp中求出的next数组,本身就是具有这个结构,不过next数组中
求出的是当前的最大前缀等于后缀的前缀子串的位置。
那么只要递归输出这个next数组就行了。
代码:
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
#define MAX 400005
int next[MAX];
char s[MAX];
int len;
void get_next()
{
int i=,j=-;
next[i]=j;
for(i=;i<len;i++)
{
while(j>-&&s[j+]!=s[i])
j=next[j];
if(s[j+]==s[i]) j++;
next[i]=j;
}
}
void output(int x)
{
if(next[x]==-)
{
printf("%d ",x+);
return ;
}
output(next[x]);
printf("%d ",x+);
//cout<<"ok"<<endl;
}
int main()
{
/*ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
while(~scanf("%s",s))
{
memset(next,,sizeof(next));
len=strlen(s);
get_next();
/*for(int i=0;i<len;i++)
cout<<next[i]<<" ";
cout<<endl;*/
output(len-);
printf("\n");
}
return ;
}
 
 
 
 
 
 
 
 
 

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

  1. poj2752seek the name, seek the fame【kmp】

    The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...

  2. poj2752Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  3. POJ 2752Seek the Name, Seek the Fame(next数组妙用 + 既是前缀也是后缀)

    题目链接 题意:求一个字符串中 前缀 和 后缀 相同的长度 分析: 对于一个字符串他自己的长度肯定是可以的.然后如果满足 前缀 和 后缀相等,那个前缀 最后一个字符 一定 和 该字符串最后一个字符相等 ...

  4. POJ 2752 Seek the Name, Seek the Fame(next数组的理解)

    做此题,只要理解好next数组就行....................... #include <cstdio> #include <cmath> #include < ...

  5. POJ--2752--Seek the Name, Seek the Fame【KMP】

    链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...

  6. POJ 2752 Seek the Name, Seek the Fame next数组理解加深

    题意:给你一个字符串,寻找前缀和后缀相同的子串(包括原串). 从小到大排列输出其子串的长度 思路:KMP  next 数组应用. 其实就是一个数学推导过程. 首先由next数组 可知s(ab) = s ...

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

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

  8. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

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

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

随机推荐

  1. Windows程序设计--(五)绘图基础

    5.1 GDI的结构 图形设备接口(GDI:Graphics Device Interface)是Windows的子系统,它负责在视讯显示器和打印机上显示图形. 5.2 设备环境 5.2.1 获取设备 ...

  2. 记录每个action执行时间

    import org.apache.commons.lang.time.StopWatch; import org.aspectj.lang.JoinPoint; import org.aspectj ...

  3. Boyer-Moore

    Boyer-Moore 只做这些失败的匹配,就可以排除掉相应的对齐位置.在BM算法中,模式串P与文本串T的对准位置依然自左向右移动,而在对准位置确是自右向左的逐一比对各个字符串,具体的,在每一轮自右向 ...

  4. find按照文件大小查找

    例如,find -size +1M:查找大于 1 MB 的文件.其他参数: b: 512-byte blocks. This is the default if no unit is specifie ...

  5. 英语单词retrieve

    retrieve 来源——报错信息 [root@centos65 ~]# yum whatprovides */lsb_release Loaded plugins: fastestmirror, s ...

  6. 聊聊Dubbo - Dubbo可扩展机制实战

    1. Dubbo的扩展机制 在Dubbo的官网上,Dubbo描述自己是一个高性能的RPC框架.今天我想聊聊Dubbo的另一个很棒的特性, 就是它的可扩展性. 如同罗马不是一天建成的,任何系统都一定是从 ...

  7. Python基础教程(014)--缩进错误

    前言 学会编写格式 内容 print(“HelloWorld”) print(“HelloWorld”) ----缩进错误 print(“HelloWorld”) 错误信息: IndentationE ...

  8. 九、async、future、packaged_task、promise

    std::async.std::future创建后台任务并返回值. 希望线程返回一个值. std::async是个函数模板,用来启动一个异步任务,返回一个std::future对象 异步任务:自动创建 ...

  9. paper 162:卷积神经网络(CNN)解析

    卷积神经网络(CNN)解析: 卷积神经网络CNN解析 概揽 Layers used to build ConvNets 卷积层Convolutional layer 池化层Pooling Layer ...

  10. linux系统下jdk安装配置

    1.有jdk包(linux版) 2.放到linux系统下 3.建议在usr下新建jdk目录之后将jdk文件放到该目录下 3.配置系统信息   /etc/profile 需要配置的信息如下:#set j ...