POJ - 3294 Life Forms【后缀数组】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/85001403

Time limit 5000 ms
Memory limit 65536 kB

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.


题意

给定n个字符串,求一个最长公共子串,满足在超过n/2个不同的字符串中出现
若有多个都要输出


题目分析

按后缀排序的套路把n个字符串拼起来,中间隔一格特殊字符
注意这里每一次隔的特殊字符不能相同

后缀排序求height数组
二分最长公共子串的长度mid在height上判断
即把height按mid值分组
检查一个组内是否有超过n/2隔不同的sa值即可

得出最大长度后再带入height数组找所有答案就好
显然一个组内至多只会有一个可行答案

注意n==1的情况,直接特判输出这个字符串即可

扫描二维码关注公众号,回复: 4984690 查看本文章

#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;
typedef double dd;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return f*x;
}

const int maxn=200010;
int T,n,m;
int a[maxn],ASC;
int rak[maxn],sa[maxn],tp[maxn],tax[maxn];
int height[maxn],ans;
int cnt[110],rem[maxn];
char ss[1010],txt[maxn];

void rsort()
{
    for(int i=0;i<=m;++i) tax[i]=0;
    for(int i=1;i<=n;++i) tax[rak[i]]++;
    for(int i=1;i<=m;++i) tax[i]+=tax[i-1];
    for(int i=n;i>=1;--i) sa[tax[rak[tp[i]]]--]=tp[i];
}

void SA()
{
    m=1024;
    for(int i=1;i<=n;++i)
    rak[i]=a[i],tp[i]=i;
    
    rsort();
    for(int k=1;k<=n;k<<=1)
    {
        int p=0;
        for(int i=n-k+1;i<=n;++i) tp[++p]=i;
        for(int i=1;i<=n;++i) if(sa[i]>k) tp[++p]=sa[i]-k;
        
        rsort();
        swap(rak,tp);
        rak[sa[1]]=p=1;
        for(int i=2;i<=n;++i)
        rak[sa[i]]=(tp[sa[i]]==tp[sa[i-1]]&&tp[sa[i]+k]==tp[sa[i-1]+k])?p:++p;
        if(p>=n) break;
        m=p;
    }
}

void getH()
{
    int k=0;
    for(int i=1;i<=n;++i)
    {
        if(k) k--;
        int j=sa[rak[i]-1];
        while(a[i+k]==a[j+k]) k++;
        height[rak[i]]=k;
    }
}

int check(int x)
{
    memset(cnt,0,sizeof(cnt));
    int sum=1; cnt[rem[sa[1]]]++; 
    for(int i=2;i<=n;++i)
    {
        if(a[rem[sa[i]]]>=123) break;
        if(height[i]<x)
        {
            memset(cnt,0,sizeof(cnt));
            sum=1; cnt[rem[sa[i]]]++; 
        }
        else
        {
            if(++cnt[rem[sa[i]]]==1) sum++;
            if(sum*2>T) return 1;
        }
    }
    return 0;
}

void qans(int x)
{
    memset(cnt,0,sizeof(cnt));
    int sum=1,pos=sa[1],judge=1; cnt[rem[sa[1]]]++; 
    for(int i=2;i<=n;++i)
    {
        if(a[rem[sa[i]]]>=123) break;
        if(height[i]<x)
        {
            memset(cnt,0,sizeof(cnt));
            sum=1; cnt[rem[sa[i]]]++; 
            pos=sa[i]; judge=1;
        }
        else
        {
            if(++cnt[rem[sa[i]]]==1) sum++;
            if(sum*2>T&&judge)
            {
                judge=0;
                for(int j=0;j<x;++j)
                printf("%c",txt[pos+j]);
                printf("\n");
            }
        }
    }
}

int main()
{
    while(scanf("%d",&T)!=EOF)
    {
        if(T==0) break;
        if(T==1){
            scanf("%s",&ss);
            printf("%s\n\n",ss);
            continue;
        }
        
        ASC=123; ans=0;
        int sum=0,len=0;
        for(int i=1;i<=T;++i) 
        {
            scanf("%s",&ss); len=strlen(ss);
            
            for(int j=0;j<len;++j) 
            {
                a[sum+j+1]=ss[j]; 
                txt[sum+j+1]=ss[j];
                rem[sum+j+1]=i;
            }
            
            a[sum+len+1]=ASC++; 
            txt[sum+len+1]='#';
            sum+=len+1;
        }
        
        n=sum;
        SA(); getH();
        
        int L=0,R=n,mid;
        while(L<R)
        {
            mid=L+R>>1;
            if(check(mid)) ans=mid,L=mid+1;
            else R=mid;
        }
        if(ans==0) printf("?\n");
        else qans(ans);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/85001403