poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)

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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
字符串 a b a b c a b a b a b a b c a b a b
前缀表值 -1 0 0 1 2 0 1 2 3 4 3 4 3 4 5 6 7 8 9
因为字符串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代码:
 1 #include<string.h>
 2 #include<cstdio>
 3 const int maxn=4e5+5;
 4 char pattern[maxn];
 5 int out[maxn],prefix[maxn],lenb;
 6 void get_prefix_table(){
 7     int j=0,pos=-1;
 8     prefix[0]=-1;
 9     while(j<lenb){
10         if(pos==-1||pattern[pos]==pattern[j])prefix[++j]=++pos;
11         else pos=prefix[pos];
12     }
13 }
14 int main(){
15     while(~scanf("%s",pattern)){
16         lenb=strlen(pattern);
17         get_prefix_table();
18         /*for(int i=0;i<=lenb;++i)
19             printf("%3d",i);
20         printf("
");
21         for(int i=0;i<=lenb;++i)
22             printf("%3d",prefix[i]);
23         printf("
");*/
24         int k=0,j=lenb;
25         while(j!=0){out[k++]=j;j=prefix[j];}//直到最长公共前后缀长度为0为止
26         for(int i=k-1;i>=0;--i)
27             printf("%d%c",out[i],i==0?'
':' ');
28     }
29     return 0;
30 }
原文地址:https://www.cnblogs.com/acgoto/p/9453608.html