Blue Jeans

Blue Jeans

 

Time Limit:   1000MS       Memory Limit:   65535KB
Submissions:   91       Accepted:   46

 

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: 1. A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset. 2. 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
解析: 大致题意:找出最长连续的的公共字符串 大致思路:一接触这道题,最先想到的方法就是确定一字符串,分别检验是否都有,但如果从一般方法看来,很费时,代码也会很冗余。这里就用到一函数strstr(),用来判断是否包含很方便,代码简单如下:
# include<stdio.h>
# include<string.h>
int main()
{
    int nCase;
    char sign[15][65],str[65],loop[65];
    int num,i,j,k;
    int MaxLen,leap;
    scanf("%d",&nCase);
    while(nCase--)
    {
        scanf("%d",&num);
        getchar();
        MaxLen=0;
        for(i=0;i<num;i++)
        {
            scanf("%s",sign[i]);
            getchar();
        }
        for(i=0;i<=57;i++)
        {
            leap=1;
            k=2;
            str[0]=sign[0][i];//初始化字符串
            str[1]=sign[0][i+1];
            while(leap)
            {
                leap=0;
                str[k]=sign[0][i+k];//如果前面字符串满足条件,就逐次增加字符串的长度
                str[++k]='\0';//要是函数能识别,这不不能少
            for(j=1;j<num;j++)    
            {
                if(!strstr(sign[j],str))break;//strstr()查看该字符串sign[j]是否含有str,很省时
            }
                if(j==num&&i+k<=60)
                {
                    leap=1;
                    if(strlen(str)>MaxLen||strlen(str)==MaxLen&&strcmp(str,loop)<0)//按照题意,取最长公共字符串;如果等长,取字母靠前的
                    {
                        strcpy(loop,str);
                        MaxLen=strlen(str);
                    }
                }

            }
        }
            if(MaxLen)
            printf("%s\n",loop);
            else printf("no significant commonalities\n");
    }
    return 0;
}

 

posted on 2013-03-02 19:00  即为将军  阅读(241)  评论(0)    收藏  举报

导航