poj 3080 Blue Jeans(KMP匹配,枚舉子串)

來源:互聯網
上載者:User

連結:

http://poj.org/problem?id=3080


題目大意:

給出m(2<=m<=10)個DNA序列, 求出這m個序列中最長的公用字串。如果有多個相同長度的,輸出字典序最小的。


分析與總結:

依次枚舉所有的子串, 然後再看是否在所有序列中都能夠匹配。儲存下長度最大且字典序最小的序列。



代碼:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 1000005;const int N = 60;int nMax;char seq[12][85];char ans[85];int  f[85];void getFail(char *p, int *f){    int m=strlen(p);    f[0]=f[1]=0;    for(int i=1; i<m; ++i){        int j=f[i];        while(j && p[i]!=p[j])j=f[j];        f[i+1] = p[i]==p[j]?1+j:0;    }}bool find(char *T,char *p,int *f){    getFail(p,f);    int n=strlen(T);    int m=strlen(p);    int j=0;    for(int i=0; i<n; ++i){         while(j && T[i]!=p[j])j=f[j];         if(T[i]==p[j])++j;         if(j==m){             return true;         }    }    return false;}int main(){    int nCase, m;    char str[85];    scanf("%d",&nCase);    while(nCase--){        scanf("%d",&m);        for(int i=0; i<m; ++i){            scanf("%s",seq[i]);        }        bool ok=false;        nMax=3;        // 枚舉所有子串        for(int i=0; i<m; ++i){            for(int j=0; j<N-3; ++j){ //枚舉起點                memset(str, 0, sizeof(str));                for(int k=j,p=0; k<N; ++k){                    str[p++]=seq[i][k];                    if(p>=nMax){                        bool flag=false;                        for(int l=0; l<m; ++l)if(l!=i){                            if(!find(seq[l],str,f)){                                flag=true;                                break;                            }                        }                        if(!flag){                            if(p==nMax){                                if(!ok){                                    ok=true;                                    strcpy(ans,str);                                }                                else if(strcmp(ans,str)>0){                                    strcpy(ans,str);                                }                            }                            else if(p>nMax){                                nMax=p;                                strcpy(ans,str);                            }                        }                        else{                            break;  // 如果這個長度不能匹配了,那麼更長的也全部都不能匹配                        }                    }                }            }        }        if(ok)puts(ans);        else puts("no significant commonalities");    }    return 0;}


 ——  生命的意義,在於賦予它意義士。

          原創 http://blog.csdn.net/shuangde800 , By
  D_Double  (轉載請標明)


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.