Blue Jeans(串)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10083   Accepted: 4262

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

题意:寻找m个序列中共有的最长的子串,长度小于3的输出no significant commonalities,长度相等时输出字典序小的;

解题思路:枚举第一个序列的所有子串,每枚举出一个检查该子串是否是m个序列共有的,若是,比较该串与ans[]的长度取较长的,若长度相等取字典序小的;当枚举完所有子串后,ans[]保存的就是m个序列共有的最长的串。
 1 #include<stdio.h>

 2 #include<string.h>

 3 

 4 int main()

 5 {

 6     int i,j,k,n,t;

 7     char DNA[12][80],tmp[80],ans[80];

 8     scanf("%d",&t);

 9     while(t--)

10     {

11         ans[0] = '\0';

12         scanf("%d",&n);

13         for(i = 0; i < n; i++)

14             scanf("%s",DNA[i]);

15         for(i = 0; i < 60; i++)//枚举子串的起点

16         {

17             for(j = i+2; j < 60; j++)//枚举子串的终点

18             {

19                 int cnt = 0;

20                 for(k = i; k <= j; k++)

21                 {

22                     tmp[cnt++] = DNA[0][k];

23                 }

24                 tmp[cnt] = '\0';//得到一个子串

25                 for(k = 1; k < n; k++)

26                 {

27                     if(strstr(DNA[k],tmp) == NULL)

28                         break;

29                 }

30                 if(k < n) continue;

31                 if(strlen(ans) == strlen(tmp))

32                 {

33                     if(strcmp(ans,tmp) > 0)

34                         strcpy(ans,tmp);

35                 }

36                 else

37                 {

38                     if(strlen(tmp) > strlen(ans))

39                         strcpy(ans,tmp);

40                 }

41             }

42         }

43         if(strlen(ans) < 3) printf("no significant commonalities\n");

44         else printf("%s\n",ans);

45     }

46     return 0;

47 }
View Code
 
  

 

 

你可能感兴趣的:(JE)