Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串
題意:給一個字符串S, 求出所有前綴pre,使得這個前綴也正好是S的后綴。 輸出所有前綴的結束位置。
就是求前綴和后綴相同的那個子串的長度 ?然后從小到大輸出,主要利用next數組求解。
例如 “ababcababababcabab”, 以下這些前綴也同時是S的后綴
ab ?: ?位置2
abab ?: 位置4
ababcabab : 位置9
ababcababababcabab : 位置 18
分析與總結:
這題,關鍵在于對KMP的失配函數的理解。只要真正理解了,那么做出來完全不成問題。
next[i]的意義就是:前面長度為i的字串的【前綴和后綴的最大匹配長度】?
下面是后來在網上找的一個圖片,很形象. ?
 ?? ? ? ?
? ? ? ? ? ? ??
?
??????e.g.
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| dp | a | b | a | b | c | a | b | a | b | a | b | a | b | c | a | b | a | b | /0 | 
| next | -1 | 0 | 0 | 1 | 2 | 0 | 1 | 2 | 3 | 4 | 3 | 4 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 
??????Out: 2 ?4 ?9 ?18
 ??????由于子串既是S的前綴又是后綴,所以除去字符串本身長度18外,次長的為在字符串結束符‘\0’處求得的next數組值9,
??????此時S的前綴為 ababcabab(012345678),S的后綴為 ababcabab(9-17)。接著找第三長的既是S的前綴又是S后綴的子串。
??????如果找next[17]處的值8,則不滿足是S后綴的要求,因為17本身的字符是被排除在外的,10-16亦是同理。
??????而對于9之前的子串有next[18]知既是S的前綴又是S的后綴。而next[9]的值4指明了位置在9前的子串的前后綴長度為4,
??????而該子串包含在S的前綴中,所以該子串的后綴也包含在S的前綴中,而S的前綴又是S的后綴,所以該子串的后綴也是S的后綴。
??????依次往前直到滿足條件的子串長度為0為止。
題目:
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 aaaaaSample Output
2 4 9 18 1 2 3 4 5AC代碼
#include<stdio.h> #include<string.h> using namespace std; const int M=4e5+10; char dp[M]; int s[M],e[M]; void f(int x) {memset(s,0,sizeof(s));int i=0;int j=-1;s[0]=-1;while(i<x){if(j==-1||dp[i]==dp[j])s[++i]=++j;else j=s[j];} } int main() {while(~scanf("%s",dp)){memset(e,0,sizeof(e));int x=strlen(dp);f(x);int b=0,k,a=0,i,flag=0;i=x;while(i!=-1){e[a++]=i;i=s[i];if(i==0)break;}for(i=a-1; i>=1; i--)printf("%d ",e[i]);printf("%d\n",e[0]);}return 0; }?
總結
以上是生活随笔為你收集整理的Seek the Name, Seek the Fame POJ - 2752 (理解KMP函数的失配)既是S的前缀又是S的后缀的子串的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Millenium Leapcow PO
- 下一篇: 微软 Xbox 游戏“黑五”折扣上线:《
