poj3294Life Forms

来源:互联网 发布:泰州学院教务网络系统 编辑:程序博客网 时间:2024/05/18 01:12
Life Forms
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 11322 Accepted: 3135

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, titledThe 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

3abcdefgbcdefghcdefghi3xxxyyyzzz0

Sample Output

bcdefgcdefgh?

Source

Waterloo Local Contest, 2006.9.30
给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串。
算法分析:
将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,
求后缀数组。然后二分答案,将后缀分成若干组,判断每组
的后缀是否出现在不小于 k 个的原串中。这个做法的时间复杂度为 O(nlogn)。

***唯一需要注意的便是如果要你按字典序上升输出,便应该以排名枚举height
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;const int maxn=200000+100;int Belong[maxn];int s[maxn];int num,ansnum;int ans[maxn];bool vis[maxn];char s1[maxn];int sa[maxn],t[maxn],t2[maxn],c[maxn],n;//构造字符串s的后缀数组,每个字符值必须为0~m-1void build_sa(int m){    int *x=t,*y=t2;    //基数排序    for(int i=0;i<m;i++)    c[i]=0;    for(int i=0;i<n;i++)    c[x[i]=s[i]]++;    for(int i=1;i<m;i++)    c[i]+=c[i-1];    for(int i=n-1;i>=0;i--) sa[--c[x[i]]]=i;    for(int k=1;k<=n;k<<=1){        int p=0;        //直接利用sa数组排序第二关键字        for(int i=n-k;i<n;i++)  y[p++]=i;        for(int i=0;i<n;i++)    if(sa[i]>=k)    y[p++]=sa[i]-k;        //基数排序第一关键字        for(int i=0;i<m;i++)    c[i]=0;        for(int i=0;i<n;i++)    c[x[y[i]]]++;        for(int i=1;i<m;i++)    c[i]+=c[i-1];        for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        //根据sa和y计算新的x数组        swap(x,y);        p=1;        x[sa[0]]=0;        for(int i=1;i<n;i++)            x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if(p>=n)            break;        m=p;                //下次基数排序的最大值    }}int rank1[maxn],height[maxn];void getHeight(){    int i,j,k=0;    for(i=0;i<=n;i++)    rank1[sa[i]]=i;    for(i=0;i<n;i++){        if(k)            k--;        int j=sa[rank1[i]-1];        while(s[i+k]==s[j+k])   k++;        height[rank1[i]]=k;    }}bool get_divid(int beg,int end1){    int cnt=0;    memset(vis,false,sizeof(vis));    for(int i=beg;i<=end1;i++){ //排名        if(vis[Belong[sa[i]]]==false){            cnt++;            vis[Belong[sa[i]]]=true;            if(cnt>num/2)                return true;        }    }    return false;}bool check(int m){    bool res=false;    int beg=0,end1=0;    for(int i=1;i<=n;i++){        while(i<n&&height[i]>=m){            i++;            end1++;        }        if(end1-beg>=num/2){            if(get_divid(beg,end1)){                if(!res){                    ansnum=0;                    res=true;                }                ans[ansnum++]=beg;            }        }        end1=beg=i;    }    if(end1-beg>=num/2){        if(get_divid(beg,end1)){            if(!res){                ansnum=0;                res=true;            }             ans[ansnum++]=beg;        }    }    return res;}int main(){    int case1=0;    while(scanf("%d",&num)!=EOF){        if(num==0)            break;        if(case1!=0)            printf("\n");        case1++;        int cnt=0;        int num_case=1;        int high=0;        for(int i=0;i<num;i++){            scanf("%s",s1);            int len=strlen(s1);            high=max(high,len);            for(int j=0;j<len;j++){                Belong[cnt]=i;                s[cnt++]=s1[j]-'a'+110;            }            if(i!=num-1)                s[cnt++]=num_case++;        }        s[cnt++]=0;        n=cnt;        build_sa(200);        n--;        getHeight();        int max_len=0;        int low=1;        ansnum=0;        while(high-low>=0){            int mid=(high+low)>>1;            if(check(mid)){                low=mid+1;                max_len=mid;            }            else                high=mid-1;        }        if(ansnum==0)            printf("?\n");        else{            for(int i=0;i<ansnum;i++){                int j=sa[ans[i]];                for(int k=0;k<max_len;k++)                    printf("%c",s[j+k]-110+'a');                printf("\n");            }        }    }    return 0;}/*4acbdesadbxcvnasjkhegcxbnmzbcasgtwyeuxabnscmd*/


0 0
原创粉丝点击