首页 > 技术文章 > 【POJ 3080 Blue Jeans】

Damitu 2017-10-13 16:44 原文

 

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 19026
Accepted: 8466

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

 

 

【翻译】给出一些字符串,找出最长公共连续子串,并且输出字典序最小的那个,如果最优解长度小于3,输出no significant commonalities。

 

题解:

       ①串长度不超过60,因此考虑暴力枚举第一个串的子串。

       ②在上文的情况下,将当前枚举的子串与剩下的字符串匹配就可以了。

       ③为了找到字典序最小,这里直接用STL-string操作比较方便。

#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define ro(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=100;string ans,tmp;
int t,n,m,C,len,ok,OK,f[N];
char s[15][N],T[N],P[N];
bool KMP_Matching()
{
	int j;f[0]=f[1]=0;
	go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}j=0;
	go(i,0,n-1){while(j&&P[j]!=T[i])j=f[j];if((j+=P[j]==T[i])==m)return 1;}
	return 0;
}
int main()
{	
	scanf("%d",&C);
	while(C--&&scanf("%d",&t))
	{
		go(i,1,t)scanf("%s",s[i]);
		ans="";len=strlen(s[1]);
		ro(k,len,1)
		{	
			OK=0;
			go(i,0,len-k)
			{		
				ok=1;m=0;
				go(u,i,i+k-1)P[m++]=s[1][u];
				go(j,2,t)
				{
					n=strlen(s[j]);	
					go(u,0,n-1)T[u]=s[j][u];
					if(!(ok&=KMP_Matching()))break;
				}
				if(ok)
				{
					OK=1;tmp.clear();go(u,0,m-1)tmp+=P[u];
					if(ans==""||tmp<ans)ans=tmp;
				}
			}
			if(OK)
			{
				if(ans.length()>=3)cout<<ans<<endl;
				else puts("no significant commonalities");goto Judy;
			}		
		}
		puts("no significant commonalities");Judy:;
	}
	return 0;
}//Paul_Guderian

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

谁能告诉我那奔腾的迷惘与骄傲,

是否就是我心底永隔一世的河流。——————汪峰《河流》

推荐阅读