【后缀数组】【**个人的难点题**】

19 篇文章 0 订阅
13 篇文章 0 订阅

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7504 Accepted: 2060

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

AC代码:
貌似效率速度很高:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100005
#define LL long long
#define maxn 200005
using namespace std;
//以下为倍增算法求后缀数组
int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];
int cmp(int *r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
void da(const int *r,int *sa,int n,int m){
	int i,j,p,*x=wa,*y=wb,*t;
	for(i=0;i<m;i++) Ws[i]=0;
	for(i=0;i<n;i++) Ws[x[i]=r[i]]++;
	for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
	for(i=n-1;i>=0;i--) sa[--Ws[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++) Ws[i]=0;
		for(i=0;i<n;i++) Ws[wv[i]]++;
		for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
		for(i=n-1;i>=0;i--) sa[--Ws[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++;
	}
	return;
}
int sa[maxn],Rank[maxn],height[maxn];
//求height数组
void calheight(const int *r,int *sa,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++);
	return;
}
char ch[maxn];
int str[maxn];
int l[105];
int in[maxn];
int k;
bool check(int mid,int n,int out){
	bool flag[105];
	int cnt=0;
	memset(flag,false,sizeof(flag));
	for(int i=1;i<=n;i++){
		if(height[i]<mid) {//开辟一个新的组那么一组中每个后缀的前mid个字符是相等的
			if(cnt>k/2&&out){
				for(int j=0,r=sa[i-1];j<mid;j++)//等同于:for(int j=0,r=sa[i-2];j<mid;j++)
					printf("%c",str[j+r]+'a'-1);
				printf("\n");
			}
			cnt=0;
			memset(flag,false,sizeof(flag));
		}
		else{//分组:一组中每个后缀的前mid个字符是相等的 
		    //所以当一个串(拼接前的串)有一个字符在后缀中时
		//    就说明目前得到的公共串一定出现在该串中
			if(!flag[in[sa[i-1]]]){
				flag[in[sa[i-1]]]=true;
				cnt++;
			}
			if(!flag[in[sa[i]]]){
				flag[in[sa[i]]]=true;
				cnt++;
			}
			if(!out&&cnt>k/2) return true;
		}
	}
	return false;
}
int main(){
	int cnt=0;
    //freopen("in.txt","r",stdin);
	while(scanf("%d",&k)!=EOF&&k){
		int n=0;
		if(cnt) printf("\n");
		cnt++;
		for(int i=0;i<k;i++){
			scanf("%s",ch);
			int l=strlen(ch);
			for(int j=0;j< l;j++){
				str[n]=ch[j]-'a'+1;
				in[n]=i;//当时做的时候主要不太明白的地方就是in数组:将总的字符串分成k段
				n++;
			}
			str[n]=27+i;
			n++;
		}
		n--;
		str[n]=0;
		da(str,sa,n+1,26+k);
		calheight(str,sa,n);
		int low=0,high=1000,mid,ans=-1;
		while(low<=high){
			mid=(low+high)/2;
			if(check(mid,n,0)){ans=mid;low=mid+1;}
			else high=mid-1;
		}
		if(ans<=0) puts("?");
		else check(ans,n,1);
	}
	return 0;
}



如下的代码一直re:
不解:将来有时间再看一下。。。。
望路过的大神解释一下。。不胜感激!
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
#define INF 10000000
using namespace std;
#include <stdio.h>
#include <string.h>
/*
待排序的字符串放在r 数组中,从r[0]到r[n-1],长度为n,且最大值小
于m。为了函数操作的方便,约定除r[n-1]外所有的r[i]都大于0, r[n-1]=0。
函数结束后,结果放在sa 数组中,从sa[0]到sa[n-1]。
*/
const int MAX=200010;
int wa[MAX],wb[MAX],wv[MAX],ws[MAX];
int sa[MAX];
int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(char *r,int *sa,int n,int m)//注意此处的char*r有时可以改成int* r
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0;i<m;i++)
        ws[i]=0;
    for(i=0;i<n;i++)
        ws[x[i]=r[i]]++;
    for(i=1;i<m;i++)
        ws[i]+=ws[i-1];
    for(i=n-1;i>=0;i--)
        sa[--ws[x[i]]]=i;
    for(j=1,p=1;p<n;j<<=1,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++)
            ws[i]=0;
        for(i=0;i<n;i++)
            ws[wv[i]]++;
        for(i=1;i<m;i++)
            ws[i]+=ws[i-1];
        for(i=n-1;i>=0;i--)
            sa[--ws[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++;
    }
    return ;
}
int rank[MAX],height[MAX];
void calheight(char *r,int *sa,int n)//sa[i]代表后缀i在数组中的下标
{
    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++);
    return ;
}

char str[MAX];
char ans[MAX];
int d[MAX][20];
void RMQ_init_min(int n)
{
  for(int i = 1;i<=n;++i)
  d[i][0] = height[i];
  for(int j = 1;(1<<j)<=n;++j)
   for(int i = 1;i+(1<<j)-1<=n;++i)
    d[i][j] = min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}

int RMQ_min(int l,int r)
{
    int k = 0;
    while((1<<(k+1))<= r-l+1) ++k;
    return min(d[l][k],d[r-(1<<k)+1][k]);
}

char input[MAX];
int flag[MAX];
int k;
int in[MAX];
bool ok(int len ,int n,int tag)//tag作为一个标志传进去0代表判断1代表打印
{
    int cnt = 0;
    memset(flag,0,sizeof(flag));
    for(int i = 1;i<n;++i)
    {
        if(height[i]<len)// divide into some parts
        {
           if(cnt>k/2&&tag)
           {
             for(int j = 0,r = sa[i-1];j<len;++j)
             printf("%c",str[j+r]);
             printf("\n");
           }
           memset(flag,0,sizeof(flag));
           cnt = 0;
        }else
        {
            if(!flag[in[sa[i-1]]])
            {
                flag[in[sa[i-1]]] = 1;
                cnt++;
            }
            if(!flag[in[sa[i]]])
            {
                flag[in[sa[i]]] = 1;
                cnt++;
            }

            if(cnt>k/2&&!tag)
            return true;
        }
    }
    return false;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int first = 1;
    while(scanf("%d",&k)==1)
    {
        if(k==0)
        break;
        memset(str,0,sizeof(str));
        if(!first)
        printf("\n");
        first = 0;
        int t = 0;
        for(int i = 0;i<k;++i)// connect the string
        {
          scanf("%s",input);
          int l = strlen(input);
          for(int j = 0;j<l;++j)
          {
              str[t] = input[j];
              in[t] = i;
              ++t;
          }
          str[t] = 123 + i;
          t++;
        }
        t--;
        str[t] = 0;

         da(str,sa,t+1,'z'+123+k-1);
         calheight(str,sa,t);
         int L = 0;
         int R = 1000;
         int tag = 0;
         int ans = -1;
         while(R >= L)
         {
            int mid = L + (R - L)/2;
            if(ok(mid,t+1,0))
            {
               L = mid+1;
               ans = mid;
            }
            else
            R = mid-1;
         }

       if(ans<=0)
       printf("?");
       else
       ok(ans,t+1,1);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值