博客园 首页 私信博主 显示目录 隐藏目录 管理 动画

poj 3294

Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12688   Accepted: 3552

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

?

二分长度把height分组,同组在不同的n/2个串里就是答案,只需记录该组中任意一个后缀的起点
SA尤其要注意开够数组,免得连接串的时候出错
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MN 200003
using namespace std;

int n,m,nm,nnm;
char s1[MN];
int s[MN],a[MN];
int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0,len,nu[MN],st[MN];
bool w[101];
inline void gr(int x){
    rank[sa[1]]=1;
    for (int i=2;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1;
    for (int i=1;i<=n;i++) s[i]=rank[i];
}
inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e5;i++)v[i]+=v[i-1];}
inline void gsa(){
    gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0);
    for (int i=1;i<n;i<<=1){
        gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
        for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j;
        for (int j=1;j<=n;j++) sa[j]=q[j];gr(i);
        if (rank[sa[n]]==n) return;
    }
}
inline void gh(){for (int i=1,k=0,j;i<=n;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
int main(){
    scanf("%d",&n);
    while(n){
        nm=0;
        for (int i=1;i<=n;i++){
            scanf("%s",s1);
            m=strlen(s1);
            for (int j=0;j<m;j++) a[++nm]=s1[j]-'a',nu[nm]=i;a[++nm]=26+i;
        }
        nnm=n;
        n=nm;
        for (int i=1;i<=nm;i++) s[i]=a[i];
        gsa();gh();
        int l=0,r=n,mid,bo=1,i,j,k,mmh,pos;
        while(l<r){
            mid=(l+r+1)>>1;
            for (i=1,j,k=2;i<=n;i=k++){
                memset(w,0,sizeof(w));mmh=0;
                while (h[k]>=mid&&k<=n) k++;
                for (j=i;j<k;j++) if (!w[nu[sa[j]]]&&nu[sa[j]]) mmh++,w[nu[sa[j]]]=1;
                if (mmh*2>nnm) break;
            }
            if (i<=n) l=mid;else r=mid-1;
        }
        pos=0;
        for (i=1,j,k=2;i<=n;i=k++){
            memset(w,0,sizeof(w));mmh=0;
            while (h[k]>=l&&k<=n) k++;
            for (j=i;j<k;j++) if (!w[nu[sa[j]]]&&nu[sa[j]]) mmh++,w[nu[sa[j]]]=1;
            if (mmh*2>nnm) st[++pos]=sa[i];
        }
        if (l==0) printf("?\n");else
        for (int i=1;i<=pos;putchar('\n'),i++)
        for (int j=0;j<l;j++) putchar(a[st[i]+j]+'a');
        putchar('\n');
        scanf("%d",&n);
    }
}
3952K 750MS G++ 2114B

 


posted @ 2016-05-05 17:42  swm_sxt  阅读(207)  评论(0编辑  收藏  举报