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. RabbitMQ Hello World

    RabbitMQ Hello World rabbitmq operation: C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.2\sbin ...

  2. Asp.net MVC 简单分页 自做简单分页

    Asp.net MVC 简单分页:   public static string Pager(int page,int pageSize,int total)         {           ...

  3. fedora下安装xdot和objgraph

    前提:安装好了python 1.先下载xdot-0.6.tar.gz和objgraph-1.8.0-py27-none-any.whl,你也可以在官网上下载其他版本. 2.下载完后,解压. 3.打开终 ...

  4. 深度学习笔记之使用Faster-Rcnn进行目标检测 (原理篇)

    不多说,直接上干货! Object Detection发展介绍 Faster rcnn是用来解决计算机视觉(CV)领域中Object Detection的问题的.经典的解决方案是使用: SS(sele ...

  5. python3 base64模块代码分析

    #! /usr/bin/env python3 """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data ...

  6. C++ 函数部分(2)

    C++函数的递归调用 函数可以直接或间接地调用自身,称为递归调用.所谓直接调用自身,就是指在一个函数的函数体中出现了对自身的调用表达式,例如: void fun1(void) { //do somet ...

  7. Spring Boot Spring 自动配置

    Spring Boot 不是应用服务器: Spring Boot没有实现诸如JPA.JMS(Java Message Service)之类的Java企业级规范: Spring Boot没有引入任何形式 ...

  8. 在Orchard CMS Theme 用代码定义布局Widgets 配置

    在上篇中主要详细的叙述了代码的编写,这一篇主要讲解配置.可能有人会有疑问,在上一篇的代码里只有对数据的展示部分的编写,并没有提供数据源.这就是Orchard的强大之处,数据源是通过在后台配置的,那有人 ...

  9. Uvalive 4043 Ants —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...

  10. ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling

    ValueError: Some of types cannot be determined by the first 100 rows, please try again with sampling ...