[POJ3080] Blue Jeans

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.

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

Source

South Central USA 2006

题解

这题可以用\(KMP\)或后缀数组,虽然后缀数组运算比较快,但是后缀数组常用的倍增求\(sa[],rk[]\)的时间复杂度为\(O(n*log_2n)\),所以这题我选择用\(O(n)\)\(KMP\)

我们枚举第\(1\)个串的后缀与其他的串分别比较,用\(KMP\)求出最长匹配长度,统计最长匹配长度的最小值,将最小值设为\(Res_i\),显而易见,最后的最大长度为\(max\{Res_i|i≤n,i∈N^*\}\)

  • 贡献一次\(WA\):注意字典序
#include<iostream>
#include<cstdio>
using namespace std;

const int L=120;
int n,lp,ls,nt[L];
char s[12][L],p[L],out[L];

void Make_nt()
{
	int i=0,j=-1;
	nt[i]=-1;
	while(i<lp)
	{
		if(j==-1||p[i]==p[j])
			++i,++j,nt[i]=j;
		else j=nt[j];
	}
}

int KMP(char s[])
{
	int i=0,j=0,Res=0;
	while(i<ls&&j<lp)
	{
		if(j==-1||s[i]==p[j]) ++i,++j;
		else j=nt[j];
		Res=max(Res,j);
	}
	return Res;
}

void Check(int i,int Res)
{
	for(int j=0;j<Res;++j)
	{
		if(out[j]<s[1][i+j]) return;
		if(out[j]>s[1][i+j]) break;
	}
	for(int j=0;j<Res;++j) out[j]=s[1][i+j];
}

void Solve()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%s",s[i]);
	lp=ls;
	int Ans=0,Res;
	for(int i=0;i<ls-3;++i,--lp)
	{
		for(int j=i;j<ls;++j) p[j-i]=s[1][j];
		Make_nt();
		Res=lp;
		for(int j=2;j<=n;++j)
		{
			Res=min(Res,KMP(s[j]));
			if(Res<3) break;//优化
		}
		if(Res>Ans)
		{
			Ans=Res;
			for(int j=0;j<Res;++j) out[j]=s[1][i+j];
		}
		if(Res==Ans) Check(i,Ans);
	}
	if(Ans<3) puts("no significant commonalities");
	else
	{
		for(int i=0;i<Ans;++i) printf("%c",out[i]);
		putchar('\n');
	}
}

int main()
{
	int T; ls=60;
	for(scanf("%d",&T);T;--T) Solve();
	return 0;
}
posted @ 2020-01-19 16:04  OItby  阅读(160)  评论(0编辑  收藏  举报