Blue Jeans
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:12233 |
|
Accepted:5307 |
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 cocould 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
32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample output
no significant commonalitiesAGATACCATCATCAT
Source
South Central USA 2006 enumerative s [0] indicates the length of each segment, which matches the n-1 values below to see if it is a string. It is retained and has the longest length. If the length is the same, the minimum Lexicographic Order is required. (If the length is less than 3, no significant commonalities is output ). For continuous substring matching, you can also use subsequence matching. However, the longest diagonal line is used for the continuous length.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int mm[100][100] ;char s[100][100] , str1[100] , str2[100] ;int f(int i,int j,int k,int num){ int ii , jj , ll ,flag; ll = strlen(s[k]); memset(mm,0,sizeof(mm)); flag = 0 ; for(ii = i ; ii <= j ; ii++) { for(jj = 0 ; jj < ll ; jj++) { if(ii == i || jj == 0) { if( s[0][ii] == s[k][jj] ) mm[ii][jj] = 1 ; } else { if( s[0][ii] == s[k][jj] ) mm[ii][jj] = 1 ; if( mm[ii][jj] ) mm[ii][jj] += mm[ii-1][jj-1] ; if( mm[ii][jj] == num ) flag = 1 ; } } } if( flag ) return 1 ; else return 0 ;}int main(){ int t , n , i , j , l , ll , k , num , flag , max1 , a , b ; scanf("%d", &t); while(t--) { scanf("%d", &n); for(i = 0 ; i < n ; i++) scanf("%s", s[i]); l = strlen(s[0]); max1 = 0 ; for(i = 0 ; i < l ; i++) { for(j = l-1 ; j-2 >= i ; j--) { num = j - i + 1 ; for(k = 1 ; k < n ; k++) { if( !f(i,j,k,num) ) break; } if(k == n ) { if(num > max1) { max1 = num ; for(a = 0 ; a < num ; a++) str1[a] = s[0][a+i] ; str1[a] = '\0' ; } else if( num == max1 ) { for(a = 0 ; a < num ; a++) str2[a] = s[0][a+i] ; str2[a] = '\0' ; if( strcmp(str1,str2) > 0 ) strcpy(str1,str2); } } } } if( max1 < 3 ) printf("no significant commonalities\n"); else { printf("%s\n", str1); } } return 0;}