poj 3294 后缀数组 多字符串中不小于 k 个字符串中的最长子串

Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 16223 Accepted: 4763

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 个字符串,求出现在多于 (n/2)个字符串中的最长子串,按照字典序输出所有的解。没有就输出“?”。
代码:
//论文题,将 n 个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开,求后缀数组。然后二分答案,将后缀
//分成若干组,判断每组的后缀是否出现在不小于 k 个的原串中。这个做法的时间复杂度为 O(nlogn)。
//数组要开大一些不然re。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=300000;
int sa[MAXN+9],he[MAXN+9],ra[MAXN+9],xx[MAXN+9],yy[MAXN+9],buc[MAXN+9];
int s[MAXN+9],id[MAXN+9],vis[1009],q[1009];
int len,m,top;
void get_suf()
{
    int *x=xx,*y=yy;
    for(int i=0;i<m;i++) buc[i]=0;
    for(int i=0;i<len;i++) buc[x[i]=s[i]]++;
    for(int i=1;i<m;i++) buc[i]+=buc[i-1];
    for(int i=len-1;i>=0;i--) sa[--buc[x[i]]]=i;
    for(int k=1;k<=len;k<<=1){
        int p=0;
        for(int i=len-1;i>=len-k;i--) y[p++]=i;
        for(int i=0;i<len;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
        for(int i=0;i<m;i++) buc[i]=0;
        for(int i=0;i<len;i++) buc[x[y[i]]]++;
        for(int i=1;i<m;i++) buc[i]+=buc[i-1];
        for(int i=len-1;i>=0;i--) sa[--buc[x[y[i]]]]=y[i];
        swap(x,y);
        p=1;x[sa[0]]=0;
        for(int i=1;i<len;i++){
            if(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k])
                x[sa[i]]=p-1;
            else x[sa[i]]=p++;
        }
        if(p>=len) break;
        m=p;
    }
    for(int i=0;i<len;i++) ra[sa[i]]=i;
    int k=0;
    for(int i=0;i<len;i++){
        if(ra[i]==0) { he[0]=0; continue; }
        if(k) k--;
        int j=sa[ra[i]-1];
        while(s[i+k]==s[j+k]&&i+k<len&&j+k<len) k++;
        he[ra[i]]=k;
    }
}
bool solve(int mid,int n)
{
    memset(vis,0,sizeof(vis));
    int l=0,qq[1009],cnt=0,st=-1;
    for(int i=1;i<len;i++){
        if(he[i]<mid){
            if(cnt>n/2&&st!=-1) qq[++l]=st;
            memset(vis,0,sizeof(vis));
            cnt=0;st=-1;
        }else{
            if(st==-1) st=i-1;
            if(!vis[id[sa[i]]]) cnt++;
            vis[id[sa[i]]]=1;
            if(!vis[id[sa[i-1]]]) cnt++;
            vis[id[sa[i-1]]]=1;
        }
    }
    if(cnt>=n/2&&st!=-1) qq[++l]=st;
    if(l){
        top=l;
        for(int i=1;i<=l;i++) q[i]=qq[i];
        return 1;
    }else return 0;
}
int main()
{
    int n;
    char ch[2000];
    while(scanf("%d",&n)&&n){
        len=0;
        top=0;
        int r=0,l=0,ans=0;
        for(int i=1;i<=n;i++){
            scanf("%s",ch);
            int tmp=strlen(ch);
            r=max(r,tmp);
            for(int j=0;j<tmp;j++){
                s[len]=ch[j]-'a';
                id[len++]=i;
            }
            s[len]=i+30;
            id[len++]=0;
        }
        m=200;
        get_suf();
        while(l<=r){
            int mid=(l+r)>>1;
            if(solve(mid,n)) { ans=mid;l=mid+1; }
            else r=mid-1;
        }
        if(ans==0){
            printf("?\n\n");
            continue;
        }
        for(int i=1;i<=top;i++){
            for(int j=sa[q[i]];j<=sa[q[i]]+ans-1;j++) printf("%c",s[j]+'a');
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/7619824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值