您的位置:首页 > 产品设计 > UI/UE

Blue Jeans(POJ--3080

2015-08-11 20:21 447 查看
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.
题意:总共T组数据,每组数据输入m表示有m个字符串,求这m个字符串的最长公共子串,如果有多种情况则输出字典序较小的那个子串;如果没有则输出“no significant commonalities”.
思路:由于数据不是很大直接暴力即可。枚举第一个字符串的每个子字符串与下边m-1个字符串匹配,看是不是其子串,如果是且比之前的子串长度长或字典序小就保存这个子串最后输出。
Sample Input
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output
no significant commonalities
AGATAC
CATCATCAT

#include <cstdio>
#include <cstring>
using namespace std;
char dna[20][100],st[100],obj[100];
int main()
{
//freopen("oo.text","r",stdin);
int T,m,i,j,k;
scanf("%d",&T);
while(T--)
{
memset(obj,0,sizeof(obj));
scanf("%d",&m);
for(i=0; i<m; i++)
scanf("%s",dna[i]);
for(i=3; i<=60; i++)                    //枚举子串的长度,据题目要求,子串长度不小于3才算子串
for(j=0; j<=60-i; j++)      //从第一个字符串的第一个位置开始向后枚举子串
{
strcpy(st,dna[0]+j);  //复制第一个字符串第j个位置之后的所有字母
st[i]=0;                       //只要长度为i的子串
int cnt=1;
for(k=1; k<m; k++)
if(strstr(dna[k],st))   //如果该子串也是后边字符串的子串则strstr的返回值是该子串在字符串中第一个字母的位置(strstr函数中的参数是有顺序的,第一个参数指的是被匹配的字符串,第二个参数指的是匹配的子串)
cnt++;
if(cnt==m)
{
if(strlen(obj)!=i||strcmp(obj,st)>0)
strcpy(obj,st);
}
}
if(!obj[0])
printf("no significant commonalities\n");
else
printf("%s\n",obj);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: