• poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串


    Life Forms
    Time Limit: 5000MS   Memory Limit: 65536K
    Total Submissions: 16223   Accepted: 4763

    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

    题意:
    给定 n 个字符串,求出现在多于 (n/2)个字符串中的最长子串,按照字典序输出所有的解。没有就输出“?”。
    代码:
    //论文题,将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组。然后二分答案,将后缀
    //分成若干组,判断每组的后缀是否出现在不小于 k 个的原串中。这个做法的时间复杂度为 O(nlogn)。
    //数组要开大一些不然re。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int MAXN=300000;
    int sa[MAXN+9],he[MAXN+9],ra[MAXN+9],xx[MAXN+9],yy[MAXN+9],buc[MAXN+9];
    int s[MAXN+9],id[MAXN+9],vis[1009],q[1009];
    int len,m,top;
    void get_suf()
    {
        int *x=xx,*y=yy;
        for(int i=0;i<m;i++) buc[i]=0;
        for(int i=0;i<len;i++) buc[x[i]=s[i]]++;
        for(int i=1;i<m;i++) buc[i]+=buc[i-1];
        for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i;
        for(int k=1;k<=len;k<<=1){
            int p=0;
            for(int i=len-1;i>=len-k;i--) y[p++]=i;
            for(int i=0;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
            for(int i=0;i<m;i++) buc[i]=0;
            for(int i=0;i<len;i++) buc[x[y[i]]]++;
            for(int i=1;i<m;i++) buc[i]+=buc[i-1];
            for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i];
            swap(x,y);
            p=1;x[sa[0]]=0;
            for(int i=1;i<len;i++){
                if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
                    x[sa[i]]=p-1;
                else x[sa[i]]=p++;
            }
            if(p>=len) break;
            m=p;
        }
        for(int i=0;i<len;i++) ra[sa[i]]=i;
        int k=0;
        for(int i=0;i<len;i++){
            if(ra[i]==0) { he[0]=0; continue; }
            if(k) k--;
            int j=sa[ra[i]-1];
            while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
            he[ra[i]]=k;
        }
    }
    bool solve(int mid,int n)
    {
        memset(vis,0,sizeof(vis));
        int l=0,qq[1009],cnt=0,st=-1;
        for(int i=1;i<len;i++){
            if(he[i]<mid){
                if(cnt>n/2&&st!=-1) qq[++l]=st;
                memset(vis,0,sizeof(vis));
                cnt=0;st=-1;
            }else{
                if(st==-1) st=i-1;
                if(!vis[id[sa[i]]]) cnt++;
                vis[id[sa[i]]]=1;
                if(!vis[id[sa[i-1]]]) cnt++;
                vis[id[sa[i-1]]]=1;
            }
        }
        if(cnt>=n/2&&st!=-1) qq[++l]=st;
        if(l){
            top=l;
            for(int i=1;i<=l;i++) q[i]=qq[i];
            return 1;
        }else return 0;
    }
    int main()
    {
        int n;
        char ch[2000];
        while(scanf("%d",&n)&&n){
            len=0;
            top=0;
            int r=0,l=0,ans=0;
            for(int i=1;i<=n;i++){
                scanf("%s",ch);
                int tmp=strlen(ch);
                r=max(r,tmp);
                for(int j=0;j<tmp;j++){
                    s[len]=ch[j]-'a';
                    id[len++]=i;
                }
                s[len]=i+30;
                id[len++]=0;
            }
            m=200;
            get_suf();
            while(l<=r){
                int mid=(l+r)>>1;
                if(solve(mid,n)) { ans=mid;l=mid+1; }
                else r=mid-1;
            }
            if(ans==0){
                printf("?
    
    ");
                continue;
            }
            for(int i=1;i<=top;i++){
                for(int j=sa[q[i]];j<=sa[q[i]]+ans-1;j++) printf("%c",s[j]+'a');
                printf("
    ");
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    eazsy-ui 按钮样式
    sql
    事务
    spring-aop切入点配置
    改变HTML文件上传控件样式(隐藏默认样式 用点击图片间接调用)
    JS的几条规则
    JS高阶函数
    JAVA中的工厂方法模式和抽象工厂模式
    JAVA单例模式
    JAVA对象创建的过程
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/7619824.html
Copyright © 2020-2023  润新知