KMP POJ 2752 Seek the Name, Seek the Fame

Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15562 Accepted: 7875
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
Source
POJ Monthly–2006.01.22,Zeyuan Zhu

暴力求解,TLE:

#include<stdio.h>
#include<stdlib.h> 
#include<algorithm>
#include<iostream>
#define MAX 400005 
using namespace std;
char in[MAX];
int ans[MAX],len,num,flag; 
int main(){
    freopen("i.txt","r",stdin); 
    while(cin>>in){
        len=strlen(in);
        num=0;       
        for(int i=1;i<=len;i++){
            flag=1;
            for(int j=1;j<=i;j++){
                if(in[len-j]!=in[i-j]){
                    flag=0;
                    break;
                }
            }
            if(flag) ans[num++]=i; 
        } 
        for(int i=0;i<num;i++)  cout<<ans[i]<<" ";
        cout<<endl;
    } 
    return 0;
}

利用KMP的getnext()函數,得到優化的程序:

//3172K 469MS
#include<stdio.h>
#include<stdlib.h> 
#include<algorithm>
#include<iostream>
#define MAX 400005 
using namespace std;
char s[MAX];
int next[MAX],ans[MAX],len,num=0;
void get_next(char *s,int*next){
    memset(next,0,sizeof(next));
    next[0]=-1;
    len=strlen(s);
    int suf=0,pre=-1;
    while(suf!-len) {
        if(pre==-1||s[suf]==s[pre]) next[++suf]=++pre;
        else pre=next[pre];
    }
}
int main(){
    freopen("i.txt","r",stdin);
    while(~scanf("%s",s)){
        get_next(s,next);  
        ans[0]=len;
        while(next[len]>0){
          ans[++num]=next[len];
          len=next[len];
        }
        for(int i=num;i>0;i--) 
            printf("%d ",ans[i]);
        printf("%d\n",ans[0]);
    }
}

要理解這樣做的正確性,重點是理解next數組的含義:next[i]表示模式串前i個字符最大
相同前綴和後綴(不包括字符串本身)的長度。
從next數組最後一個開始往前尋找每個相同的後綴和前綴。
next數組:
這裏寫圖片描述
假設next[len]=k,表示模式串前k個組成的前綴和最後k個字符組成後綴的是相同的。
那麼下一個相同的後綴和前綴,最後一個字符一定相同,因爲後綴的最後一個字符一定不會變,假設字符串最後一個字符用紅色表示,那麼所有的前綴的最後一個都應該是紅色的,如圖所示。因此,可以通過len=next[len]=k來尋找下一個相同的前綴和後綴。

這裏寫圖片描述


模式串的前k個和後k個相同,前k個的前m個和後m個相同,等價代換得到模式串的前m個和後m個相同,於是又得到了一個相同的前綴和後綴。


歡迎留言,積極討論,一起進步!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章