POJ 3294_

Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 13157 Accepted: 3701
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

?

很经典的后缀数组组题目,求多个串满足k/2个公共子串并将其输出

首先我们用loc数组存下每个串的长度之后
我们可以先二分公共子串的长度,通过hegiht【i】去判断,因为如果height[i]大于Mid那么才有成为子串的可能。并且在height[i]》mid时判断这两个串(sa[i] 和sa[i-1])为开头的后缀是属于哪个串中的。

在结束二分后如果left==1时那么则说明没有答案输出 ?

具体请看代码

#include <stdio.h>
#include <algorithm>
#include <string.h>
#define maxs 101000
#define MME(i,j) memset(i,j,sizeof(i))
using namespace std;
int s[maxs];
int sa[maxs],rank[maxs],height[maxs];
int wa[maxs],wb[maxs],wv[maxs],wd[maxs];
char input[maxs];
int loc[maxs];
int cmp(int *r,int a,int b,int k)
{
    return r[a]==r[b]&&r[a+k]==r[b+k];
}
void get_sa(int *r,int n,int m)
{
    int i,j,p,*x=wa,*y=wb;
    for(i=0; i<m; i++) wd[i]=0;
    for(i=0; i<n; i++) wd[x[i]=r[i]]++;
    for(i=1; i<m; i++) wd[i]+=wd[i-1];
    for(i=n-1; i>=0; i--) sa[--wd[x[i]]]=i;

    for(j=1,p=1; p<n; j*=2,m=p)
    {
        for(p=0,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<n; i++) wv[i]=x[y[i]];
        for(i=0; i<m; i++) wd[i]=0;

        for(i=0; i<n; i++) wd[wv[i]]++;
        for(i=1; i<m; i++) wd[i]+=wd[i-1];

        for(i=n-1; i>=0; i--) sa[--wd[wv[i]]]=y[i];

        for(swap(x,y),p=1,x[sa[0]]=0,i=1; i<n; i++)
        {

            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
        }
    }
    /*for(int i=0; i<n; i++)
        printf("SA[%d] is %d\n",i,sa[i]);*/
}

void build_height(int *r,int n)
{
    int i,j,k=0;
    for(i=1; i<=n; i++) rank[sa[i]]=i;
    for(i=0; i<n; height[rank[i++]]=k)
    {

        for(k?k--:0,j=sa[rank[i]-1]; r[i+k]==r[j+k]; k++);
    }

   /* for(int i=0; i<=n; i++)
        printf("h[%d] is %d\n",i,height[i]);*/
}
bool flag=0,vis[105];
int ans[maxs];
bool check(int mid,int len,int k) //mid为二分答案,len为总长度,k为总共的字符串数
{
    int top=0;
    MME(vis,0);
    int tot=0,j;
    for(int i=1;i<=len;i++)
    {
        if(height[i]>=mid)
        {
            for(j=1; j<=k; j++)
            {
                if(sa[i]>loc[j-1]&&sa[i]<loc[j]) tot+= (vis[j]?0:1) ,vis[j]=1;
                if(sa[i-1]>loc[j-1]&&sa[i-1]<loc[j]) tot+= (vis[j]?0:1),vis[j]=1;
            }
        }
        else
        {
            if(tot>k/2)
             ans[++top]=sa[i-1];
             tot=0;
             MME(vis,0);
        }
    }
    if(tot>k/2)
    {
        ans[++top]=sa[len];
    }
    if(top)
      {
            ans[0]=top;
            return 1;
      }
    return 0;
}
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        int templen=0,len,pos;
        for(int i=1;i<=n;i++)
        {
            scanf("%s",input+templen);
            //len=strlen(input);
            for(;input[templen]!='\0';)
            {
                s[templen]=input[templen];
                templen++;
            }

            s[templen]='#'+i;
            loc[i]=templen;
            templen++;
            /*printf("%d  is   %c\n",i,'#'+i);
            for(int i=0;i<templen;i++)
            printf("%c",s[i]);

            puts("");*/
        }

        s[templen-1]=0;


       /* for(int i=1;i<=n;i++)
            printf("%d is %d\n",i,loc[i]);
        puts("");*/
        get_sa(s,templen,255);
        build_height(s,templen-1);

        int left=1,right=templen,mid;

        while(right>=left)
        {
            mid=(left+right)/2;
            if(check(mid,templen,n))
            {
                left=mid+1;
            }
            else right=mid-1;
        }

        if(flag)
           puts("");
        flag=1;
        if(left==1)
            printf("?\n");
        else
        {
                int j;
            for(int i=1;i<=ans[0];i++)
            {
                for(j=ans[i];j<ans[i]+left-1;j++)
                        printf("%c",s[j]);

                puts("");
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值