现在的位置: 首页 > 综合 > 正文

POJ 3294 Life Forms

2014年09月05日 ⁄ 综合 ⁄ 共 3303字 ⁄ 字号 评论关闭

二分长度,统计在哪些串中出现。。。

Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

[]  
[Go Back]   [Status]  

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

[]  
[Go Back]   [Status]  

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAX=2100,MIN=210,maxn=210000;

char str[MIN][MAX],s[maxn];
int sa[maxn],rank[maxn],rank2[maxn],c[maxn],height[maxn],*x,*y;

bool cmp(int*r ,int a,int b,int len,int n)
{
    if(r[a]==r[b]&&a+len<n&&b+len<n&&r[a+len]==r[b+len]) return true;
    return false;
}

void radix_sort(int n,int sz)
{
    for(int i=0;i<sz;i++) c[i]=0;
    for(int i=0;i<n;i++) c[x[y[i]]]++;
    for(int i=1;i<sz;i++) c[i]+=c[i-1];
    for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
}

void get_sa(char *s,int n,int sz=128)
{
    x=rank;y=rank2;
    for(int i=0;i<n;i++) x[i]=s[i],y[i]=i;

    radix_sort(n,sz);

    for(int len=1;len<n;len*=2)
    {
        int yid=0;

        for(int i=n-len;i<n;i++) y[yid++]=i;
        for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len;

        radix_sort(n,sz);

        swap(x,y);
        x[sa[0]]=yid=0;
        for(int i=1;i<n;i++)
        {
            x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid;
        }

        sz=yid+1;
        if(sz>=n) break;
    }

    for(int i=0;i<n;i++) rank[i]=x[i];
}

void get_height(char *str,int n)
{
    int k=0;height[0]=0;
    for(int i=0;i<n;i++)
    {
        if(rank[i]==0) continue;
        k=max(k-1,0);
        int j=sa[rank[i]-1];
        while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++;
        height[rank[i]]=k;
    }
}

int maxlen,minlen,tot,ans[maxn],color[maxn],vis[MIN],n;

void solve(int len,int * ans,int limit)
{
    int low=0,mid,high=limit,beg,num;
    int half=n/2;

    while(low<=high)
    {
        int cnt=0,flag=0;
        mid=(low+high)/2;
        for(int i=1;i<len;i++)
        {
            if(height[i]<mid)
            {
                memset(vis,0,sizeof(vis));
                vis[color[sa[i]]]=1;
                flag=0; beg=sa[i+1]; num=1;
            }
            else
            {
                if(vis[color[sa[i]]]==0) vis[color[sa[i]]]=1,num++;
                if(num>half&&flag==0)
                {
                    flag=1; ans[cnt++]=beg;
                }
            }
        }
        if(cnt)
        {
            maxlen=mid;low=mid+1;tot=cnt;
        }
        else
        {
            high=mid-1;
        }
    }
}

int main()
{
    tot=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        int k=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",str[i]);
            if(strlen(str[i])>minlen) minlen=strlen(str[i]);
            for(int j=0;str[i][j];j++)
            {
                s[k]=str[i][j],color[k++]=i+1;
            }
            s[k++]=(i+1)%95+1;s[k]=0;
        }
        if(n==1) {printf("%s\n\n",str[0]);continue;}
        get_sa(s,strlen(s)); get_height(s,strlen(s));
        solve(strlen(s),ans,minlen);
        if(maxlen==0)
        {
            puts("?");
        }
        else
        {
            for(int i=0;i<tot;i++)
            {
                for(int j=ans[i];j<ans[i]+maxlen;j++)
                    putchar(s[j]); putchar(10);
            }
        }
        putchar(10);
    }
    return 0;
}

抱歉!评论已关闭.