poj 2752(拓展KMP模板题)
Posted violet-acmer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 2752(拓展KMP模板题)相关的知识,希望对你有一定的参考价值。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 24399 | Accepted: 12731 |
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:)
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.
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=400010; 6 7 int nex[maxn]; 8 int extend[maxn]; 9 char buf[maxn]; 10 11 void Get_Next(const char T[]) 12 { 13 int len=strlen(T); 14 int j=0; 15 nex[0]=len; 16 while(j+1 < len && T[j+1] == T[j]) 17 j++; 18 nex[1]=j; 19 int k=1; 20 for(int i=2;i < len;++i) 21 { 22 int P=nex[k]+k-1; 23 int L=nex[i-k]; 24 if(i+L <= P) 25 nex[i]=L; 26 else 27 { 28 j=max(0,P-i+1); 29 while(i+j < len && T[i+j] == T[j]) 30 j++; 31 nex[i]=j; 32 k=i; 33 } 34 } 35 } 36 void Get_Extend(const char S[],const char T[]) 37 { 38 Get_Next(T); 39 int n=strlen(S); 40 int m=strlen(T); 41 int j=0; 42 while(j < n && j < m && S[j] == T[j]) 43 j++; 44 extend[0]=j; 45 int k=0; 46 for(int i=1;i < n;++i) 47 { 48 int P=extend[k]+k-1; 49 int L=nex[i-k]; 50 if(i+L <= P) 51 extend[i]=L; 52 else 53 { 54 j=max(0,P-i+1); 55 while(i+j < n && j < m && S[i+j] == T[j]) 56 j++; 57 extend[i]=j; 58 k=i; 59 } 60 } 61 } 62 63 int main() 64 { 65 while(~scanf("%s",buf)) 66 { 67 Get_Extend(buf,buf); 68 int len=strlen(buf); 69 for(int i=len-1;i > 0;--i) 70 if(i+extend[i] == len) 71 printf("%d ",extend[i]); 72 printf("%d ",len); 73 } 74 return 0; 75 }
以上是关于poj 2752(拓展KMP模板题)的主要内容,如果未能解决你的问题,请参考以下文章
POJ_2752 Seek the Name, Seek the Fame KMP