zoukankan      html  css  js  c++  java
  • POJ 3294不小于k个字符串中的最长子串

    不小于k个字符串中的最长子串

    Description

    You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.

    The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant's life forms ended up with a large fragment of common DNA.

    Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.

    Input

    Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.

    Output

    For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.

    Sample Input

    3
    abcdefg
    bcdefgh
    cdefghi
    3
    xxx
    yyy
    zzz
    0

    Sample Output

    bcdefg
    cdefgh
    
    ?
    

    Source


    Sol

    考虑广义SAM

    维护出每一个点出现次数。

    暴力把所有可能的点拿出来,二分hash sort就好了

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define maxn 2000006
    #define ll unsigned long long 
    #define P 793999
    using namespace std;
    int n,rt,la,cnt,len[105];
    char ch[105][5005];
    int num[maxn],now[maxn];
    struct sam{
        int Max,par,nex[26];
        int id,x;
    }s[maxn];
    struct node{
        int len;
        char ch[5005];ll h[5005];
        void H(){
            for(int j=1;j<=len;j++)h[j]=h[j-1]*P+ch[j];
        }
    }ans[10005];
    bool operator ==(node A,node B){
        if(A.len!=B.len)return 0;
        return A.h[A.len]==B.h[B.len];
    }
    int find(node A,node B){
        int M=min(A.len,B.len);
        if(A.ch[1]!=B.ch[1])return 0;
        int l=1,r=M;
        while(l<r){
            int mid=(l+r+1)>>1;
            if(A.h[mid]==B.h[mid])l=mid;
            else r=mid-1;
        }
        return l;
    }
    bool operator <(node A,node B){
        int x=find(A,B);
        if(min(A.len,B.len)==x)return A.len<B.len;
        return A.ch[x+1]<B.ch[x+1];
    }
    void init(){
        for(int i=1;i<=cnt;i++){
            for(int j=0;j<26;j++)s[i].nex[j]=0;
            s[i].Max=s[i].par=0;
            num[i]=now[i]=0;
            s[i].id=s[i].x=0;
        }
        rt=la=cnt=1;
    }
    void ins(int c,int x){
        int np=++cnt,p=la;la=np;s[np].Max=s[p].Max+1;
        s[np].id=s[np].Max;s[np].x=x;
        for(;p&&!s[p].nex[c];p=s[p].par)s[p].nex[c]=np;
        if(!p)s[np].par=rt;
        else {
            int q=s[p].nex[c],nq;
            if(s[q].Max==s[p].Max+1)s[np].par=q;
            else {
                nq=++cnt;s[nq].Max=s[p].Max+1;
                s[nq].id=s[np].id;s[nq].x=x;
                for(int j=0;j<26;j++)s[nq].nex[j]=s[q].nex[j];
                s[nq].par=s[q].par;s[q].par=s[np].par=nq;
                for(;p&&s[p].nex[c]==q;p=s[p].par)s[p].nex[c]=nq;
            }
        }
    }
    int main(){
        while(~scanf("%d",&n)){
            if(!n)break;
            init();
            for(int i=1;i<=n;i++){
                scanf(" %s",ch[i]+1);
                len[i]=strlen(ch[i]+1);
                la=rt;
                for(int j=1;j<=len[i];j++)ins(ch[i][j]-'a',i);
                cout<<len[i]<<endl;
            }
            for(int i=1;i<=n;i++){
                int k=rt;
                for(int j=1;j<=len[i];j++){
                    k=s[k].nex[ch[i][j]-'a'];
                    int p=k;
                    for(;now[p]!=i&&p;p=s[p].par)now[p]=i,num[p]++;
                }
            }
            int Max=0;
            for(int i=1;i<=cnt;i++){
                if(num[i]*2>n)Max=max(Max,s[i].Max);
            }
            int tp=0;
            for(int i=2;i<=cnt;i++){
                if(s[i].Max==Max&&num[i]*2>n){
                    int l=0;tp++;
                    for(int j=s[i].id-Max+1;j<=s[i].id;j++){
                        ans[tp].ch[++l]=ch[s[i].x][j];
                    }
                    ans[tp].len=l;ans[tp].H();
                }
            }
            if(!tp)puts("?");
            else {
                sort(ans+1,ans+tp+1);
                for(int i=1;i<=tp;i++){
                    if(ans[i]==ans[i-1])continue;
                    for(int j=1;j<=ans[i].len;j++)printf("%c",ans[i].ch[j]);puts("");    
                }
            }
            puts("");
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    CentOS7搭建FTP服务器和安装FTP客户端
    Python实现网络和IP地址计算
    [Leetcode Weekly Contest]270
    [Leetcode Weekly Contest]269
    [Leetcode Weekly Contest]266
    Vue.use原理及源码解读
    Rust 程序设计语言 6
    go 语言的 Context
    Rust程序设计语言(7)
    手机界面设计中12种常用布局转载
  • 原文地址:https://www.cnblogs.com/liankewei/p/12285186.html
Copyright © 2011-2022 走看看