poj 3294 求多于k个字符串的最长公共子串的个数-------后缀数组+二分答案


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

?


把这些字符串的连起来,中间隔特殊字符(特殊字符不能相同),注意此时数组要开大,因为多了很多特殊字符。从0到某个最短字符串二分答案。(如题是求的只需要是k个以上的字符串的最长公共子串,所以是最长字符串二分答案,如果是全部串的话,只需要将k变成字符串的数量)

lenk[i]数组标记着连接之后的第i个字符串结束的特殊字符的位置


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
const int MAX=100100;
int wa[MAX],wb[MAX],wsf[MAX],wv[MAX],sa[MAX];
int rank[MAX],height[MAX],r[MAX];
int lenk[110];
int ans[MAX];
bool visited[110];
int k;
int n;
int cmp(int *r,int a,int b,int k){
    return (r[a]==r[b])&&(r[a+k]==r[b+k]);
}
void da(int *r,int *sa,int n,int m)//此处N比输入的N要多1,为人工添加的一个字符,用于避免cmp时越界
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0;i<m;i++) wsf[i]=0;
    for(i=0;i<n;i++) wsf[x[i]=r[i]]++;
    for(i=1;i<m;i++) wsf[i]+=wsf[i-1];
    for(i=n-1;i>=0;i--) sa[--wsf[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++) wsf[i]=0;
        for(i=0;i<n;i++) wsf[wv[i]]++;
        for(i=1;i<m;i++) wsf[i]+=wsf[i-1];
        for(i=n-1;i>=0;i--) sa[--wsf[wv[i]]]=y[i];
        for(t=x,x=y,y=t,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++;
    }
}
void calheight(int *r,int *sa,int n)//得到height数组,
{
    int i,j,k=0;
    for(i=1;i<=n;i++)
        rank[sa[i]]=i; //名次是从1~n,sa[]=(0~n-1)位置
    for(i=0;i<n;i++)
    {
        if(k)  k--;
        else  k=0;
        j=sa[rank[i]-1];
        while(r[i+k]==r[j+k])
        k++;
        height[rank[i]]=k;
    }
}
bool check(int x)
{
    int cnt=0;
    int size=0;
    memset(visited,0,sizeof(visited));
    for(int i=2;i<=n;i++)
    {
        if(height[i]>=x)
        {
            for(int j=1;j<=k;j++)
            {
                if(sa[i-1]>lenk[j-1]&&sa[i-1]<lenk[j])//找寻属于哪个字符串并标记
                    cnt+=(visited[j]?0:1),visited[j]=1;
                if(sa[i]>lenk[j-1]&&sa[i]<lenk[j])
                    cnt+=(visited[j]?0:1),visited[j]=1;
            }
        }
        else//此时小于x,统计之前的公共子串是否符合题意
        {
            if(cnt>k/2)
                ans[++size]=sa[i-1];
            cnt=0;
            memset(visited,0,sizeof(visited));
        }
    }
    if(cnt>k/2)
        ans[++size]=sa[n];
    if(size>0)
    {
        ans[0]=size;
        return true;
    }
    return false;
}
int main()
{
    bool flag=0;
    while(cin>>k)
    {
        if(k==0)
            return 0;
        string str;
        n=0;
        memset(lenk,0,sizeof(lenk));
        int maxx=0;
        lenk[0]=-1;
        for(int i=1;i<=k;i++)
        {
            cin>>str;
            int len=str.length();
            maxx=max(maxx,len);
            for(int j=0;j<=len-1;j++)
                r[n++]=(int)str[j];
            lenk[i]=n;
            r[n++]=128+i;//特殊字符不能相同
        }
        n=n-1;
        r[n]=0;
        da(r,sa,n+1,300);
        calheight(r,sa,n);
        int ll=0,rr=maxx;
        while(ll<=rr)
        {
            int mid=(ll+rr)/2;
            if(check(mid))
                ll=mid+1;
            else
                rr=mid-1;
        }//ll-1才是公共子串的长度
        if(flag==0)
            flag=1;
        else
            cout<<endl;
        if(ll==1)
            cout<<"?"<<endl;
        else
        {
            for(int i=1;i<=ans[0];i++)
            {
                for(int j=ans[i];j<=ans[i]+ll-2;j++)
                    cout<<(char)r[j];
                cout<<endl;
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值