Pku3080 Blue Jeans

Pku3080 Blue Jeans

Time Limit:1000MS  Memory Limit:265536K
Total Submit:4 Accepted:3

Description

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated.

As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers.

A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC.

Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.


给出n个长度为60的DNA基因(A腺嘌呤 G鸟嘌呤 T胸腺嘧啶 C胞嘧啶)序列,求出他们的最长公共子序列

Input

Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
m lines each containing a single base sequence consisting of 60 bases.

Output

For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

Sample Input

3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT

以前是拿kmpA过的,现在学后缀数组,就用后缀数组再刷了一遍。稍微修改了一下后缀排序,代码变短,速度还快了。

思路:把每个字符串接起来,之间用不同的且没有出现的字符隔开,然后就是和求最长出现k次重复子串一样的方法一样了,判断它是否在每个原串中都出现过即可。
 
 
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxl=1010;
int sa[maxl],t1[maxl],t2[maxl],rank[maxl],h[maxl],sum[maxl],tot,n,t,st,ans,stp;
char s[maxl];

void clear(){
	ans=0,memset(sa,0,sizeof(0));memset(rank,0,sizeof(rank));memset(h,0,sizeof(h)); memset(t1,0,sizeof(t1));memset(t2,0,sizeof(t2));memset(sum,0,sizeof(sum));
}

void getsa(){
	int *x=t1,*y=t2,p=0,m=255;
	for (int i=1;i<=n;i++) sum[x[i]=s[i]]++;
	for (int i=1;i<=m;i++) sum[i]+=sum[i-1];
	for (int i=1;i<=n;i++) sa[sum[x[i]]--]=i;
	for (int j=1;p<n;j<<=1,m=p){p=0;

		for (int i=n-j+1;i<=n;i++) y[++p]=i;
		for (int i=1;i<=n;i++) if (sa[i]>j) y[++p]=sa[i]-j;
		memset(sum,0,sizeof(sum));
		for (int i=1;i<=n;i++) sum[x[y[i]]]++;
		for (int i=1;i<=m;i++) sum[i]+=sum[i-1];
		for (int i=n;i;i--) sa[sum[x[y[i]]]--]=y[i];
		swap(x,y);x[sa[1]]=p=1;
		for (int i=2;i<=n;i++) {
			if (y[sa[i]]!=y[sa[i-1]]||y[sa[i]+j]!=y[sa[i-1]+j]) p++;
			x[sa[i]]=p;
		}
	}
	memcpy(rank,x,sizeof(rank));
}

void geth(){
	for (int i=1,j=0;i<=n;i++){
		if (rank[i]==1) continue;

		while (s[i+j]==s[sa[rank[i]-1]+j]) j++;
		h[rank[i]]=j;
		if (j) j--; 
	}
}

bool check(int lim){
	bool flag[15];memset(flag,0,sizeof(flag));
	int cnt=0,st=sa[1];
	for (int i=2;i<=n;i++){
		if (h[i]>=lim){
			if (!flag[sa[i]/61+1]){
				flag[sa[i]/61+1]=true;
				cnt++;
			}
			if (!flag[sa[i-1]/61+1]){
				flag[sa[i-1]/61+1]=true;
				cnt++;
			}
			if (cnt==tot){

				stp=st;
				return true;
			}
		}
		else{
			memset(flag,0,sizeof(flag));cnt=0;st=sa[i];
		}
	}
	return false;
}

bool bs(){
	int l=3,r=61,mid=(l+r)>>1;
	while (l<=r){
		if (check(mid)){ans=mid,l=mid+1;}
		else r=mid-1;
		mid=(l+r)>>1;
	}
	return ans;
}

int main(){
	scanf("%d",&t);
	while (t--){
		clear();
		scanf("%d",&tot);
		st=1;
		for (int i=1;i<=tot;i++){
			scanf("%s",s+st);
			st+=60,s[st++]=i;
		}
		s[st--]=0;

		n=strlen(s+1);
		//printf("%d\n",n);
		//printf("%s\n",s+1);
		getsa(),geth();

//for (int i=1;i<=n;i++) printf("%d ",h[i]);puts("");if (!bs()) printf("no significant commonalities\n");else{for (int i=stp;i<=stp+ans-1;i++) putchar(s[i]);puts("");}}fclose(stdout);// for (;;);return 0;}

你可能感兴趣的:(Pku3080 Blue Jeans)