poj 3294 Life Forms (后缀数组)

本文介绍了一种使用后缀数组解决多个字符串中最长公共子串问题的方法。通过连接多个字符串并利用后缀数组的数据结构特性,文章提出了一种有效的算法来找出超过一半输入字符串所共有的最长子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Life Forms
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 15125 Accepted: 4437

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

[Submit]   [Go Back]   [Status]   [Discuss]

题目大意:求不小于K个字符串中的最长公共子串

题解:后缀数组

将N个串连接起来,然后二分答案对height分组,看某组中是否存在K个不同串的子串。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 200003
using namespace std;
int sa[N],rank[N],height[N],xx[N],yy[N],*x,*y;
int n,m,len,p,vis[N],cnt,q[N],b[N],a[N],ans,pos[N];
char s[N];
void init()
{
	memset(sa,0,sizeof(sa));
	memset(rank,0,sizeof(rank));
	memset(height,0,sizeof(height));
	memset(b,0,sizeof(b));
	memset(q,0,sizeof(q));
	memset(vis,0,sizeof(vis));
}
int cmp(int i,int j,int l)
{
	return y[i]==y[j]&&(i+l>len?-1:y[i+l])==(j+l>len?-1:y[j+l]);
}
void get_SA()
{
	x=xx; y=yy; m=200;
	for (int i=1;i<=len;i++) b[x[i]=a[i]]++;
	for (int i=1;i<=m;i++) b[i]+=b[i-1];
	for (int i=len;i>=1;i--) sa[b[x[i]]--]=i;
    for (int k=1;k<=len;k<<=1) {
    	p=0;
    	for (int i=len-k+1;i<=len;i++) y[++p]=i;
    	for (int i=1;i<=len;i++)
    	 if (sa[i]>k) y[++p]=sa[i]-k;
    	for (int i=1;i<=m;i++) b[i]=0;
    	for (int i=1;i<=len;i++) b[x[y[i]]]++;
    	for (int i=1;i<=m;i++) b[i]+=b[i-1];
    	for (int i=len;i>=1;i--) sa[b[x[y[i]]]--]=y[i];
    	swap(x,y); p=2; x[sa[1]]=1;
    	for (int i=2;i<=len;i++) 
    	 x[sa[i]]=cmp(sa[i],sa[i-1],k)?p-1:p++;
    	if (p>len) break;
    	m=p+1;
	}
	p=0;
	for (int i=1;i<=len;i++) rank[sa[i]]=i;
	for (int i=1;i<=len;i++) {
		if (rank[i]==1) continue;
		int j=sa[rank[i]-1];
		while (i+p<=len&&j+p<=len&&a[i+p]==a[j+p]) p++;
		height[rank[i]]=p;
		p=max(p-1,0);
	}
}
int pd(int x)
{
	memset(vis,0,sizeof(vis));
	int size=1; vis[pos[sa[1]]]=1;
	int last=0;
	for (int i=2;i<=len;i++)
	 if (height[i]>=x)
	 {
	  int t=pos[sa[i]];
	  if (!vis[t]) 
	   vis[t]++,size++;
     }
     else {
     	if (size>=n/2+1) return 1;
     	for (int j=last;j<=i-1;j++)
     	 vis[pos[sa[j]]]=0;
     	size=1; last=i; vis[pos[sa[i]]]=1;
	 }
	if (size>=n/2+1) return 1;
	return 0;
}
void solve()
{
	memset(vis,0,sizeof(vis));
	int size=1; int last=0; vis[pos[sa[1]]]=1;
	for (int i=2;i<=len;i++)
	 if (height[i]>=ans) {
	 	int t=pos[sa[i]];
	    if (!vis[t]) 
	      vis[t]++,size++;
	 }
	 else {
	 	if (size>=n/2+1) q[++cnt]=sa[i-1];
	 	for (int j=last;j<=i-1;j++)
     	 vis[pos[sa[j]]]=0;
     	size=1; last=i; vis[pos[sa[i]]]=1;
	 }
	if (size>=n/2+1) q[++cnt]=sa[n];
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	while(true) {
		init();
		scanf("%d",&n);
		if (!n) break; len=0;
		for (int i=1;i<=n;i++) {
			scanf("%s",s+1);
			m=strlen(s+1);
			if (i!=1) a[++len]=i+27;
			for (int j=1;j<=m;j++)
			 a[++len]=s[j]-'a'+1,pos[len]=i;
		}
		get_SA();
		int l=1; int r=len; ans=0;
		while (l<=r) {
			int mid=(l+r)/2;
			if (pd(mid)) ans=max(ans,mid),l=mid+1;
			else r=mid-1;
		}
		cnt=0;
	    solve(); bool pd=false;
	    if (!ans) printf("?\n");
	    else {
	    	for (int i=1;i<=cnt;i++){
		    	 if (pos[q[i]]==pos[q[i]+ans-1])
		    	 {
				  pd=true;
		    	  for (int j=q[i];j<=q[i]+ans-1;j++)
		    	    printf("%c",a[j]+'a'-1);
		    	  printf("\n");
		         }
	        }
	        if (!pd) printf("?\n");
		}
		printf("\n");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值