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
解题思路:题目的意思就是找出给定串str中所有前缀子串和所有后缀子串完全匹配的子串长度。
首先要明确一下字符串前缀和后缀的概念:
字符串str的前缀是指从str的第一个字符开始到任意一个字符为止的子串;
字符串str的后缀是指从str的任意一个字符开始到最后一个字符为止的子串。
拿题目中字符串"alala"举个例子:
其所有前缀为a al ala alal alala
其所有后缀为a la ala lala alala
因此所有前缀子串和所有后缀子串中完全匹配的子串有a、ala、alala,其对应长度为1、3、5。
怎么计算这样匹配子串的长度呢?拿之前对模式串计算前缀表来分析一下过程:第一个样例是ababcababababcabab,len=18,其前缀表如下:
字符串的下标 0 1 3 5 6 7 8 10 11 12 13 14 15 16 17
字符串 a b a b c a b a b a b a b c a b a b
前缀表值 -1 0 0 1 0 1 2 3 3 4 3 4 5 6 7 8
因为字符串str自身就是最长的完全匹配的前后缀子串,而prefix[i]表示前i-1个字符组成的子串中最长公共前后缀长度,即str[i]前面有prefix[i]个字符与从str第一个字符开始连续prefix[i]个字符完全匹配,所以我们从后往前匹配,如果i位置前的子串有前缀和后缀匹配即prefix[i]!=0,则下一次匹配就在前缀子串找(即i=prefix[i]位置前面的子串),并且记录一下构成匹配的前缀子串的后一个位置即为字符串str前后缀完全匹配的长度,第一次为out[0]=18,如图所示。
令j=18,则下一次匹配为j=prefix[j=18]=9,out[1]=9,即此时前缀为ababcabab与后缀(下标从9开始到最后一个字符构成的子串ababcabab)是完全匹配的;
继续往前缀子串中匹配即j=prefix[j=9]=4,out[2]=4,即此时前缀子串为abab和后缀子串(下标从14开始到最后一个字符构成的子串abab)也是完全匹配的;
继续往前缀子串中匹配即j=prefix[j=4]=2,out[3]=2,即此时的前缀子串ab和后缀子串(下标从16开始到最后一个字符构成的子串ab)也是完全匹配的;
继续往前缀子串中匹配即j=prefix[j=2]=0,说明此时再无前后缀匹配,退出匹配过程,所以最终反序输出out数组为2 4 9 18。以上最终结果可以列举所有前后缀子串自行验证一下。
因此,本题的做法就是先计算好给定字符串的前缀表,再从j为字符串长度开始往前匹配,终止条件为j!=0,同时记录一下每次匹配的最长前后缀公共长度,最后反序输出即可。
AC代码:
 #include<string.h>
#include<cstdio>
const int maxn=4e5+;
char pattern[maxn];
int out[maxn],prefix[maxn],lenb;
void get_prefix_table(){
int j=,pos=-;
prefix[]=-;
while(j<lenb){
if(pos==-||pattern[pos]==pattern[j])prefix[++j]=++pos;
else pos=prefix[pos];
}
}
int main(){
while(~scanf("%s",pattern)){
lenb=strlen(pattern);
get_prefix_table();
/*for(int i=0;i<=lenb;++i)
printf("%3d",i);
printf("\n");
for(int i=0;i<=lenb;++i)
printf("%3d",prefix[i]);
printf("\n");*/
int k=,j=lenb;
while(j!=){out[k++]=j;j=prefix[j];}//直到最长公共前后缀长度为0为止
for(int i=k-;i>=;--i)
printf("%d%c",out[i],i==?'\n':' ');
}
return ;
}

题解报告:poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)的更多相关文章

  1. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

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

  3. Seek the Name, Seek the Fame POJ - 2752

    Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...

  4. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

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

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

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

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

随机推荐

  1. 主成分分析(principal components analysis)

    http://www.cnblogs.com/jerrylead/tag/Machine%20Learning/ PCA的思想是将n维特征映射到k维上(k<n),这k维是全新的正交特征.这k维特 ...

  2. 核函数以及SVM相关知识(重点)

    http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988406.html http://blog.pluskid.org/?p=685 考虑我们 ...

  3. 火狐浏览器Firefox 如何使用iMacros 自动填写网页表单

    1 我们首先访问一个想要自动填写表单的网站.我们以百度为例,右侧有登录窗口.   2 然后我们点开刚安装上的iMacros插件,一般安装之后就会自动出现在浏览器的某个地方,点击记录选项卡,再点击记录. ...

  4. Fp关联规则算法计算置信度及MapReduce实现思路

    说明:參考Mahout FP算法相关相关源代码. 算法project能够在FP关联规则计算置信度下载:(仅仅是单机版的实现,并没有MapReduce的代码) 使用FP关联规则算法计算置信度基于以下的思 ...

  5. Android +NDK+eclipse+opengl ES2.0 开启深度測试

    參考:https://www.opengl.org/discussion_boards/showthread.php/172736-OpenGL-ES-Depth-Buffer-Problem 环境: ...

  6. LoadRunner系列之—-03 用Java Vuser协议编写接口测试脚本

    待测试接口用java语言实现,且项目中调用该接口需要用专门的jar包.这种情况可以用Java Vuser协议实现接口调用脚本,类似java代码. 代码样例如下: /* * LoadRunner Jav ...

  7. 笔记本 ThinkPad E40 安装 Mac OS X 10.9.3 Mavericks 系统

    关于:自己最早接触Mac OS X系统是在一个论坛里.记得好像是2011年:那时论坛里就有人在虚拟机上执行Mac OS X 10.7系统.当时也依照论坛里的方法在虚拟机上成功装上了系统.那时開始就被苹 ...

  8. AngularJS自己定义标签加入回调函数eval()

    function helloworld(name){ console.log("hello!!!!!"+name) } var name="zhangsan"; ...

  9. c语言学习-指针探究

    1:指针定义格式:格式:变量类型 *变量名用途:指针变量用于储存地址(only),也就是根据地址值,访问对应的存储空间. 注意.int *p 只能指向int类型的数据: 例: int a = 20; ...

  10. iOS开发——高级篇——FMDB 数据库简单使用

    #import <Foundation/Foundation.h> @interface UserDB : NSObject // 把userDB设计成一个单例类 + (id)shareI ...