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 ,就输出 一串不认识的字母,否则就输出找到的那一串同样不认识的字母。


 分析:

 注意到,这个题给的数据范围很小,m不大于10,长度不大于60,吐过暴力枚举的话,复杂度大概是60*60*10*(60+60),不会爆,所以果断枚举,详细步奏见代码注释;

【AC代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=65;
int m,n;
char s[11][61]; //记录输入的串
char str[61];
char ans[66];

int main()
{
    int t;
    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[w]='\0'; //这里也注意 后面要用到strlen,所以必须以\0结尾
                for(int i=2;i<=m;++i)//遍历剩下的m-1个串
                {
                    if(!strstr(s[i],str)) //查找函数  如果str[i]母串中含有str子串返回其下标值  否则返回NULL
                    {
                        flag=0;//只要m-1个串中有一个不含有str子串  就直接跳出
                        break;
                    }
                }
                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) break; //注意这里  很重要的剪枝  如果不存在长为j的公共子串  就更不可能存在长度>J的子串 所以直接跳出
        }
        if(strlen(ans)<3) //题目要求
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);
    }
    return 0;
}



Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐