Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21078 Accepted: 9340
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
题意:找出给定字符串中都存在的最长的字典序最小的子串,若长度小于3,则输出no significant commonalities,否则输出该子串
思路:按长度递增的顺序,暴力枚举每个例子的第一个字符串的子串,然后通过strstr函数该子串验证是否存在于其他字符串中,每一步记录最长的子串,最后根据题意输出
#include<iostream>
#include<string.h>
using namespace std;
char s[11][66],str[66],ans[66];
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
memset(ans,' ',sizeof(ans));//后面求ans长度有用
for(int len=1;len<=60;len++)
{
int cnt=0;
for(int st=0;st<=60-len;st++)
{
for(int i=st;i<st+len;i++)//把一段字符串赋值给str
str[i-st]=s[0][i];
str[st+len]=' ';
int flag=1;
for(int i=1;i<n;i++)
if(!strstr(s[i],str)){//判断s[i]里面是否有str
flag=0;break;//一个不符合就退出该循环
}
if(flag)
{
cnt=1;
if(strlen(ans)<strlen(str))//取长的
strcpy(ans,str);
else if(strcmp(str,ans)<0)//取字典序小的
strcpy(ans,str);
}
}
if(!cnt)//短的都没有,长的肯定也没有
break;
}
if(strlen(ans)<3)
printf("no significant commonalities
");
else
printf("%s
",ans);
}
return 0;
}