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. Redis在windows下的环境搭建

    Redis在windows下的环境搭建 下载windows版本redis,,官方下载地址:http://redis.io/download, 不过官方没有Windows版本,官网只提供linux版本的 ...

  2. 141-FMC141-4路 250Msps/16bits ADC, FMC板卡

    FMC141-4路 250Msps/16bits ADC, FMC板卡 一.产品概述: 本板卡基于 FMC 标准板卡,实现 4 路 16-bit/250Msps ADC 功能.遵循 VITA 57 标 ...

  3. ViewMode

    一.ViewMode 实现使用场景-Model枚举的情景下, 注意:枚举声明在后台的时候,需要渲染界面,页面表格使用 Bootstrap Table插件-事先通过ajax 渲染(数据库读取值1.2.3 ...

  4. Django学习笔记-修改Django的默认的数据库

    1.修改设置settings中的配置 DATABASES = { 'default': { # 'ENGINE': 'django.db.backends.sqlite3', # 'NAME': os ...

  5. CS184.1X 计算机图形学导论(第四讲)

    一.齐次变换 1.平移变换 变换矩阵不能包含X,Y,Z等坐标变量 如果x坐标向右平移了5个单位长度,则x~=x+5.在变换矩阵中表示的时候添加一个w坐标变量.通过加入一个w坐标,可以实现平移变换 1& ...

  6. Jenkins ant打包部署

    选择项目 自由风格

  7. [Luogu1938][USACO09NOV]找工就业Job Hunt

    原题链接:https://www.luogu.org/problem/show?pid=1938 这一道题有一个比较难的点就是,这一张图上,是点上有权.既然点上有权的话,我们就不好一下子使用最短路了. ...

  8. Vue 滚动条动画

    <template> <div class="home-main"> <div class="progress-main"> ...

  9. struts2结果跳转和参数获取

    一.结果跳转方式 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC ...

  10. Heartbeat安装及配置

    1.yum源安装 yum -y install heartbeat 更新yum源 yum install epel-release -y yum源有问题,改用下载rpm包安装 2.rpm安装 下载rp ...