Seek the Name, Seek the Fame

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 7
Problem 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
 题解:让在一个串中找字串,使这个字串即是串s的前缀又是后缀,从p【len】开始p数组代表失配的字符的位置, 那么这个失配的位置前面的串即是串s的后缀了;肯定这个串本来就是前缀,那么失配的位置就可以当作长度了;
/***************************************************************************/
 
    如左图,假设黑色线来代表字符串str,其长度是len,红色线的长度代表next[len],根据next数组定义易得前缀的next[len]长度的子串和后缀next[len]长度的子串完全相同(也就是两条线所对应的位置)。我们再求出next[len]位置处的next值,也就是图中蓝线对应的长度。同样可以得到两个蓝线对应的子串肯定完全相同,又由于第二段蓝线属于左侧红线的后缀,所以又能得到它肯定也是整个字符串的后缀。

所以对于这道题,求出len处的next值,并递归的向下求出所有的next值,得到的就是答案。

所以每次只需要找此段匹配的长度就好

/***************************************************************************/

代码:用了个递归,浪了一下,竟然还没PE;kmp的原理,从len开始直接相当于了位置坐标加一,慢慢体味。。。

 #include<stdio.h>
#include<string.h>
const int MAXN=;
char s[MAXN];
int p[MAXN],len;
void getp(){
int i=,j=-;
p[]=-;
while(i<len){
if(j==-||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
void print(int x){
if(x<=)return;
print(p[x]);
printf("%d ",x);
}
int main(){int i,j;
while(~scanf("%s",s)){
len=strlen(s);
getp();
/*for(i=0;i<=len;i++)printf("%d ",p[i]);puts("");
j=len;
while(j>=0){
for(i=p[j];i<j;i++)printf("%c",s[i]);puts("");
j=p[j];
}*/
print(len);puts("");
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const int MAXN=400010;
int p[MAXN]; void getp(char* s){
int len=strlen(s);
int i=0,j=-1;
p[0]=-1;
while(i<len){
if(j==-1||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
/*
void kmp(char *s,char* m,int& ans){
int len=strlen(m);
getp(s);
int len2=strlen(s);
int j=0,i=0;
while(i<len){
if(j==-1||s[j]==m[i]){
i++;j++;
if(j==len2){
ans++;
}
}
else j=p[j];
}
}
*/
int main(){
char s[MAXN];
int a[MAXN];
int tp;
while(~scanf("%s",s)){
getp(s);
tp=0;
int len=strlen(s);
while(len>0){
a[tp++]=len;
len=p[len];
}
sort(a,a+tp);
// int k=unique(a,a+tp)-a;
for(int i=0;i<tp;i++){
if(i)P_;
PI(a[i]);
}puts("");
}
return 0;
}

  

Seek the Name, Seek the Fame(Kmp)的更多相关文章

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

  2. POJ 2752 - Seek the Name, Seek the Fame (KMP)

    题意:给一个字符串s,问s的某个前缀与后缀相同的情况时,长度是多少. 此题使用KMP的next数组解决. next数组中,j=next[i],next[i]表示S[0...i-1]的某个后缀(字符串S ...

  3. poj2406 Power Strings(kmp)

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

  4. (KMP)Seek the Name, Seek the Fame -- poj --2752

    http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536 ...

  5. poj 2752 Seek the Name, Seek the Fame (KMP纯模版)

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

  6. POJ 2751:Seek the Name, Seek the Fame(Hash)

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

  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. HDU1242 Rescue(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. lowerCaseTableNames

    数据库表,数据库名大小写铭感问题 mysql lower-case-table-names参数 线上有业务用到开源的产品,其中SQL语句是大小写混合的,而建表语句都是小写的,mysql默认设置导致这些 ...

  3. OpenGL进阶(十一) - GLSL4.x中的数据传递

    in out 对于 vertex shader,每个顶点都会包含一次,它的主要工作时处理关于定点的数据,然后把结果传递到管线的下个阶段. 以前版本的GLSL,数据会通过一些内建变量,比如gl_Vert ...

  4. ALSA音频工具amixer,aplay,arecord

    ALSA音频工具编译安装 ========================================================================1.官网http://www. ...

  5. C#入门(一):IDE

    设计流程 .NET可视化对象 创建工程的时候,会创建三个文件 Form1.cs Form1.Designer.cs Program.cs 当增加一个控件的时候,会在Form1.Designer.cs增 ...

  6. 【欧拉函数】【HDU1286】 找新朋友

    找新朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  7. eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。

    今天遇到上面的情况.网上找了半天都没有找到解决的办法.后来,仔细比对了一下我的eclipse版本和svn版本.发现svn版本真的太老了.用上新的svn后,立马就可以用了 svn - http://su ...

  8. 《JavaScript 闯关记》之函数

    函数是一段代码,它只定义一次,但可以被执行或调用任意次.在 JavaScript 里,函数即对象,程序可以随意操控它们.比如,可以把函数赋值给变量,或者作为参数传递给其他函数,也可以给它们设置属性,甚 ...

  9. UIImageView~动画播放的内存优化

    我目前学到的知识,播放动画的步骤就是下面的几个步骤,把照片资源放到数组里面,通过动画animationImage加载数组,设置动画播放的 时间和次数完成播放. 后来通过看一些视频了解到:当需要播放多个 ...

  10. linux学习笔记之进程间通信

    一.基础知识. 1:进程通信基础(interProcess Communication, IPC):管道,FIFO(命名管道),XSI IPC,POSIX 信号量. 2:管道. 1,缺陷. 1)部分系 ...