Description
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
- 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
Sample Input
3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 3 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA 3 CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalities AGATAC CATCATCAT
这道题但是真的想了太久,一直纳闷kmp怎么跑n个字符,
找出n个串中最长的公共串,并且要求字典序最大
直接枚举第一串的所有子串,然后与后面的所有串进行比较即可
拓展:<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(69, 69, 69); font-family: arial, 宋体, sans-serif, tahoma, 'Microsoft YaHei'; font-size: 14px; line-height: 24px;">strstr(str1,str2);</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(69, 69, 69); font-family: arial, 宋体, sans-serif, tahoma, 'Microsoft YaHei'; font-size: 14px; line-height: 24px;"> 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(69, 69, 69); font-family: arial, 宋体, sans-serif, tahoma, 'Microsoft YaHei'; font-size: 14px; line-height: 24px;"><pre name="code" class="cpp">#include<stdio.h> #include<string.h> #include<stdlib.h> #include<limits.h> #include<algorithm> using namespace std; int n,m; char a[15][65]; char b[65],c[65]; int main() { int T,i,j; scanf("%d",&T); while(T--) { memset(c,'\0',sizeof(c)); scanf("%d",&n); for(i=1;i<=n;i++) scanf("%s",a[i]); for(i=1;i<=60;i++) { int flag=0; for(j=0;j<=60-i;j++) { int len=0; int check=1; for(int k=j;;k++) { b[len++]=a[1][k]; if(len==i) break; } b[len]='\0'; for(int k=2;k<=n;k++) { if(!strstr(a[k],b)) { check=0; break; } } if(check) { flag=1; if(strlen(c)<strlen(b)) strcpy(c,b); if(strcmp(c,b)>0) strcpy(c,b); } } if(!flag) break; } if(strlen(c)<3) printf("no significant commonalities\n"); else printf("%s\n",c); } }