原题目:
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
中文概要:
给定长度为60的m个字符串,找它的最长公共子串,如果长度相同,输出字典序小的,如果找到的公共子串小于3 ,就输出 一串不认识的字母,否则就输出找到的那一串同样不认识的字母。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int m,n,t;
char s[11][65];
char ans[65];
char str[65];
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%s",s[i]);
memset(ans,0,sizeof(ans));//注意这里初始化,不然后面第一次strlen时会出错
for(int j=1;j<=60;j++)//子串长度
{
int cnt=0;//长度为j的公共串是否找到
for(int k=0;k<=60-j;k++)//遍历长度为j的子串
{
int flag=1;//标记剩下m-1个串中是否含有长度为j的子串
int w=0;
int ss=k;
while(ss<j+k)
str[w++]=s[1][ss++];//str记录子串中所有长度为j的字符串的样子
str[w]='\0'; //这里也注意 后面要用到strlen,所以必须以\0结尾
for(int i=2;i<=m;i++)//遍历剩下的m-1个串
{
if(!strstr(s[i],str))//查找函数 如果str[i]母串中含有str子串返回其下标值 否则返回NULL
{
flag=0;break;//只要m-1个串中有一个不含有str子串 就直接跳出
}
}
if(flag)//都存在str子串
{
cnt=1;//标记长度为j的公共子串是否存在
if(strlen(str)>strlen(ans))//长度优先
strcpy(ans,str);
else if(strcmp(str,ans)<0)//字典序小的优先
strcpy(ans,str);
}
}
if(!cnt) //注意这里很重要的剪枝 如果不存在长为j的公共子串 就更不可能存在长度>J的子串 所以直接跳出
break;
}
if(strlen(ans)<3)//题目要求
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
return 0;
}
思路:
暴力枚举,枚举所有长度,从1到60,然后再跟剩下m-1个字符串来进行查重。。。。。
主要是这个m<10,所以暴力肯定不会超时。
具体细节都注释了,自己也添加了一些注释