POJ - 3294 Life Forms (后缀数组 多串至少出现在k个串中的最长公共子串)

Life Forms

 

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

?

题目链接:http://poj.org/problem?id=3294

题目大意:给出k个字符串,求出现在大于k/2个字符串中的最长公共子串,如果有多个就输出多个,如果没有输出?

思路:和POJ-3450差不多,多个用数组记录每种公共子串的初始位置,记录种数,具体见代码

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int N=1000005;
int a[N],id[N],sa[N],rak[N],h[N],c[N],t1[N],t2[N];
bool cmp(int *f,int x,int y,int w){return f[x]==f[y]&&f[x+w]==f[y+w];}
void da(int a[], int sa[], int rak[], int h[], int n, int m)
{
    n++;
    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]=a[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]]=cmp(y, sa[i-1], sa[i], j)?p-1:p++;
        if(p>=n) break;
        m=p;
    }
    n--;
    for(i=0;i<=n;i++) rak[sa[i]]=i;
    int k=0;
    for(int i=0;i<n;h[rak[i++]]=k)
        for(k=k?k-1:k,j=sa[rak[i]-1];a[i+k]==a[j+k];k++);
}
int vis[105],pos[105],n,m,k,p;
char s[1005];
int fun(int x)
{
    memset(vis,0,sizeof(vis));
    int cnt=0,tot=0;
    for(int i=2;i<=n;i++)
    {
        if(h[i]<x)
        {
            if(cnt>k/2) pos[tot++]=sa[i-1];
            memset(vis,0,sizeof(vis));
            cnt=0;
            continue;
        }
        if(!vis[id[sa[i-1]]]) vis[id[sa[i-1]]]=1,cnt++;
        if(!vis[id[sa[i]]]) vis[id[sa[i]]]=1,cnt++;
    }
    if(cnt>k/2)pos[tot++]=sa[n];
    if(tot) p=tot;
    return tot;
}
int main()
{
    int t=0;
    while(~scanf("%d",&k)&&k)
    {
        if(t)printf("\n");
        t++,n=0,m=127+k;
        for(int i=0;i<k;i++)
        {
            scanf("%s",s);
            int l=strlen(s);
            for(int j=0;j<l;j++) a[n]=s[j],id[n++]=i;
            a[n]='#'+i,id[n++]='#'+i;
        }
        a[n]=0;
        da(a,sa,rak,h,n,m);
        int l=1,r=strlen(s),ans=0;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(fun(mid))
            {
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        if(ans)
        {
            for(int i=0;i<p;i++){
                for(int j=pos[i];j<pos[i]+ans;j++)
                printf("%c",a[j]);
                printf("\n");
            }
        }
        else printf("?\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值