POJ 3294 Life Forms (后缀数组,求出现在不少于k个字符串的最长子串)

Life Forms
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 7322   Accepted: 2011

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个字符串连接起来,中间用没有出现过的字符隔开,然后求后缀数组。

然后二分答案,进行分组,判断每组的后缀是否出现在不少于k个的原串中,

 

/*

 * poj 3294

 * 给出n个字符串,求出现在一半以上字符串的最长子串,按照字典序输出所有结果

 * 将n个字符串连接起来,中间用没有出现过的字符隔开,然后求后缀数组。

然后二分答案,进行分组,判断每组的后缀是否出现在不少于k个的原串中,

 */



#include <iostream>

#include <string.h>

#include <algorithm>

#include <stdio.h>

using namespace std;

const int MAXN=101010;



int sa[MAXN];

int t1[MAXN],t2[MAXN],c[MAXN];

int rank[MAXN],height[MAXN];



void build_sa(int s[],int n,int m)

{

    int i,j,p,*x=t1,*y=t2;

    for(i=0;i<m;i++)c[i]=0;

    for(i=0;i<n;i++)c[x[i]=s[i]]++;

    for(i=1;i<m;i++)c[i]+=c[i-1];

    for(i=n-1;i>=0;i--)sa[--c[x[i]]]=i;

    for(j=1;j<=n;j<<=1)

    {

        p=0;

        for(i=n-j;i<n;i++)y[p++]=i;

        for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;

        for(i=0;i<m;i++)c[i]=0;

        for(i=0;i<n;i++)c[x[y[i]]]++;

        for(i=1;i<m;i++)c[i]+=c[i-1];

        for(i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];

        swap(x,y);

        p=1;x[sa[0]]=0;

        for(i=1;i<n;i++)

            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;

        if(p>=n)break;

        m=p;

    }

}

void getHeight(int s[],int n)

{

    int i,j,k=0;

    for(i=0;i<=n;i++)rank[sa[i]]=i;

    for(i=0;i<n;i++)

    {

        if(k)k--;

        j=sa[rank[i]-1];

        while(s[i+k]==s[j+k])k++;

        height[rank[i]]=k;

    }

}

int n;

char str[110][1010];

int st[110],ed[110];//各个字符串对应的起始和结束

bool used[110];//标记

int who[MAXN];

int r[MAXN];

int check(int totlen,int len,int k)

{

    memset(used,false,sizeof(used));

    int ret=0;

    int tmp=who[sa[1]];

    if(tmp!=-1 && used[tmp]==false)

    {

        ret++;

        used[tmp]=true;

    }

    if(ret>=k)return 1;

    for(int i=2;i<=totlen;i++)

    {

        if(height[i]<len)

        {

            ret=0;

            memset(used,false,sizeof(used));

            tmp=who[sa[i]];

            if(tmp!=-1 && used[tmp]==false)

            {

                ret++;

                used[tmp]=true;

            }

            if(ret>=k)return i;

        }

        else

        {

            tmp=who[sa[i]];

            if(tmp!=-1 && used[tmp]==false)

            {

                ret++;

                used[tmp]=true;

            }

            if(ret>=k)return i;

        }

    }

    return -1;

}

void output(int totlen,int len,int k)

{

    memset(used,false,sizeof(used));

    int ret=0;

    int tmp=who[sa[1]];

    if(tmp!=-1 && used[tmp]==false)

    {

        ret++;

        used[tmp]=true;

    }

    for(int i=2;i<=totlen;i++)

    {

        if(height[i]<len)

        {

            if(ret>=k)

            {

                for(int j=0;j<len;j++)printf("%c",r[sa[i-1]+j]);

                printf("\n");

            }

            ret=0;

            memset(used,false,sizeof(used));

            tmp=who[sa[i]];

            if(tmp!=-1 && used[tmp]==false)

            {

                ret++;

                used[tmp]=true;

            }

        }

        else

        {

            tmp=who[sa[i]];

            if(tmp!=-1 && used[tmp]==false)

            {

                ret++;

                used[tmp]=true;

            }

        }

    }

    if(ret>=k)

    {

        for(int j=0;j<len;j++)printf("%c",r[sa[totlen]+j]);

        printf("\n");

    }

}

int main()

{

    //freopen("in.txt","r",stdin);

    //freopen("out.txt","w",stdout);

    int totlen;

    bool first=true;

    while(scanf("%d",&n)==1 && n)

    {

        if(first)first=false;

        else printf("\n");

        totlen=0;

        for(int i=0;i<n;i++)

        {

            scanf("%s",str[i]);

            int len=strlen(str[i]);

            for(int j=0;j<len;j++)

            {

                r[totlen+j]=str[i][j];

                who[totlen+j]=i;

            }

            r[totlen+len]=i+130;

            who[totlen+len]=-1;

            totlen+=len+1;

        }

        totlen--;

        r[totlen]=0;

        build_sa(r,totlen+1,300);

        getHeight(r,totlen);

        int k=n/2+1;

        int ans=-1;

        int left=1,right=1010;

        while(left<=right)

        {

            int mid=(left+right)/2;

            int x=check(totlen,mid,k);

            if(x==-1)

            {

                right=mid-1;

            }

            else

            {

                ans=mid;

                left=mid+1;

            }

        }

        if(ans<=0)printf("?\n");

        else

        {

            output(totlen,ans,k);

        }

    }

    return 0;

}

 

 

你可能感兴趣的:(form)