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. turtle库使用

    turtle库的使用 绘图窗体布局 turtle.setup(width,height,startx,straty) 用来控制窗体的大小与位置,其中后width与height用来控制窗体的大小,sta ...

  2. OutOfMemoryError系列(2): GC overhead limit exceeded

    原因分析 JVM抛出 java.lang.OutOfMemoryError: GC overhead limit exceeded 错误就是发出了这样的信号: 执行垃圾收集的时间比例太大, 有效的运算 ...

  3. Ubuntu 服务器Webmin错误的解决

    一:This web server is running in SSL mode. Try the URL https://***********:10000/ instead. 解决方案: 1.ss ...

  4. sublime安装LiveReload(写完代码再也不用手动刷新浏览器了)【转】

    1.首先在chrome下安装这个扩展程序 LiveReload 2.在sublime下按快捷键 ctrl+shift+p  点击截图标的位置 按回车 输入LiveReload 即可下载 然后打开 {改 ...

  5. 如何改变string中的字符值?

    string本身是不可变的,因此要改变string中字符,需要如下操作: str := “hello world” s := []byte(str) s[] = ‘o’ str = string(s) ...

  6. elasticsearch failed to load elasticsearch nodes

    转载:https://blog.csdn.net/weixin_44714808/article/details/90049315 elasticsearch 安装在linux时使用springboo ...

  7. asp.net core web api 版本控控制

    通过微软的一个库Microsoft.AspNetCore.Mvc.Versioning实现asp.net core web api的版本控制. 以两种形式组织了Controller: 文件夹分开 命名 ...

  8. Linux 多个cpp文件的编译(Makefile)

    打包so文件: CC = g++ CFLAGS=-Wall -O2 -fPIC TARGET = libbg.so SRCS := $(wildcard *.cpp) OBJS := $(patsub ...

  9. js append()和appendChild()和insertBefore()的区别

    <body> <input type="button" value="删除" id="btn"> <scrip ...

  10. 【BZOJ2946&SPOJ1812】公共串(后缀自动机)

    题意:给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 单词的数量<=5,单词的长度至少为1,最大为2000. 思路: #include<bits/stdc++.h> us ...