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. 常见Python运行时错误

    1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在 ...

  2. springMVC4(16)拦截器解析与登陆拦截模拟

    在SpringMVC中,我们会常常使用到拦截器,尽管SpringAOP也能帮我们实现强大的拦截器功能,但在Web资源供给上.却没有SpringMVC来得方便快捷. 使用SpringMVC拦截器的核心应 ...

  3. NSUserDefaults 保存自己定义对象

    项目里json返回的一个model须要保存下来,这个model是固定的没必须去创建表,想到了NSUserDefaults来存储,暂不考虑安全问题. NSUserDefaults没法直接存储一个对象.在 ...

  4. 【spring+websocket的使用】

    一.spring配置文件Java代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  5. 【拆分版】Docker-compose构建Zookeeper集群管理Kafka集群

    写在前边 在搭建Logstash多节点之前,想到就算先搭好Logstash启动会因为日志无法连接到Kafka Brokers而无限重试,所以这里先构建下Zookeeper集群管理的Kafka集群. 众 ...

  6. [CSAPP]Bufbomb实验报告

    Bufbomb实验报告 实验分析: level 0-3从test開始制运行,通过函数getbuf向外界读取一串内容(buf). Level 4 是通过參数-n,程序运行testn函数,调用getbuf ...

  7. .NET的委托和匿名函数应用一例

    闲话休提,大家都是成年人,直接上代码.有代码有J8: delegate string dlgGetTotal(); void TongJi() { dlgGetTotal getTotalInt = ...

  8. aspx后台引用不到服务器控件

    从其他地方拷贝的页面到自己的项目,后台CS代码引用hidden时,提示找不到,百度,发现可能是网站项目和Web应用程序的区别,右键aspx转化为Web应用程序即可.

  9. LiveWriter插入高亮代码插件介绍 基于SyntaxHighighter

    Codeint main() { int i; printf("%d",i); } 插件介绍 辛苦了两人小时写日志不小心浏览器崩溃了,发誓以后一定记得用Word先写好. 将Word ...

  10. 初识JVM虚拟机

    前言: Java语言里负责解释执行字节码文件的是Java虚拟机,即JVM——Java Virtual Machine(Java虚拟机). 执行Java程序的两个步骤: 由Java语言编写的程序需要进过 ...