poj3294之大于k/2个字符串中的最长子串

Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8381 Accepted: 2294

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

?
题意:求大于k/2个字符串中含有的最长公共子串

分析:后缀数组,把所有的字符串用不同的分隔符连接起来,然后求height,最后二分求长度为mid的子串是否有满足条件的,记录满足条件的所有子串的首位置,最后输出即可

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#define INF 99999999;
typedef long long LL;
using namespace std;

const int MAX=200000+10;
int *rank,r[MAX],sa[MAX],height[MAX],size;
int wa[MAX],wb[MAX],wm[MAX],len[110],pos[MAX];
bool mark[110];
char s[MAX];

bool cmp(int *r,int a,int b,int l){
	return r[a] == r[b] && r[a+l] == r[b+l];
}

void makesa(int *r,int *sa,int n,int m){
	int *x=wa,*y=wb,*t;
	for(int i=0;i<m;++i)wm[i]=0;
	for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
	for(int i=1;i<m;++i)wm[i]+=wm[i-1];
	for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
	for(int i=0,j=1,p=0;p<n;j=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<m;++i)wm[i]=0;
		for(i=0;i<n;++i)wm[x[y[i]]]++;
		for(i=1;i<m;++i)wm[i]+=wm[i-1];
		for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
		for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i){
			x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
		}
	}
	rank=x;
}

void calheight(int *r,int *sa,int n){
	for(int i=0,j=0,k=0;i<n;height[rank[i++]]=k){
		for(k?--k:0,j=sa[rank[i]-1];r[i+k] == r[j+k];++k);
	}
}

bool check(int mid,int n,int k){
	memset(mark,false,sizeof mark);
	int num=0,ans=0;
	for(int i=1;i<=n;++i){
		if(height[i]>=mid){
			for(int j=1;j<=size;++j){//判断sa[i]和sa[i-1]所属的字符串是否已被计算过 
				if(sa[i]>len[j-1] && sa[i]<len[j])ans+=(mark[j]?0:1),mark[j]=true;
				if(sa[i-1]>len[j-1] && sa[i-1]<len[j])ans+=(mark[j]?0:1),mark[j]=true;
			}
		}else{
			if(ans>k/2)pos[++num]=sa[i-1];//pos记录满足条件的子串首位置 
			ans=0;
			memset(mark,false,sizeof mark);
		}
	}
	if(ans>k/2)pos[++num]=sa[n];
	if(num){pos[0]=num;return true;} 
	return false;
}

int main(){
	int k,n,num=0;
	len[0]=-1;//注意len[0]初始化<0 
	while(cin>>k,k){
		pos[0]=size=n=0;//注意初始化 
		for(int i=0;i<k;++i){
			scanf("%s",s+n);
			for(;s[n] != '\0';++n)r[n]=s[n];
			r[len[++size]=n++]=300+i;//300+i保证每个分隔的字符不同 
		}
		r[n-1]=0;
		makesa(r,sa,n,400);
		calheight(r,sa,n-1);
		int L=1,R=n,mid;
		while(L<=R){
			mid=L+R>>1;
			if(check(mid,n,k))L=mid+1;
			else R=mid-1;
		}
		if(num++)printf("\n");
		if(L-1 == 0)printf("?\n");
		else{
			for(int i=1;i<=pos[0];++i){
				for(int j=pos[i];j<pos[i]+L-1;++j)printf("%c",s[j]);
				printf("\n");
			}
		}
		
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值