Language:
Blue Jeans
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:
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 Source /* 相当于用kmp的暴力QAQ n^2枚举第一个串的子串,然后看这个子串能不能匹配所有的串 注意:输出保证是字典序最小 */ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <string> #include <queue> #include <map> #include <stack> #include <set> #include <cmath> #include <vector> #include<cstdlib> #pragma comment (linker,"/STACK:102400000,102400000") using namespace std; #define maxn 100 int aa,bb,cc; char str1[maxn]; char str2[maxn]; int next_[maxn]; int len1,len2; struct str { char s[maxn]; int len; }node[20]; int n; int mini,minn; void get_next() { int i=0; int j=-1; while(i<len2) { if(str2[i]==str2[j] || j==-1) { ++i; ++j; next_[i]=j; } else j=next_[j]; } } int kmp(int x) { for(int i=0;i<=node[x].len;i++) str1[i]=node[x].s[i]; len1=node[x].len; int i=0; int j=0; while(i<len1) { if(j==-1 || str1[i]==str2[j]) { ++i; ++j; } else j=next_[j]; if(j==len2) { return 1; } } return 0; } int check(int a1,int b1,int a2,int b2) { for(int i=0;i<b1-a1;i++) { if(node[1].s[a1+i]<node[1].s[a2+i]) return 1; else if(node[1].s[a1+i]>node[1].s[a2+i]) return 0; } return 0; } int main() { int T; cin>>T; while(T--) { cin>>n; mini=1; minn=1000000; for(int i=1;i<=n;i++) { scanf("%s",node[i].s); node[i].len=60; } aa=bb=cc=0; for(int i=0;i<60;i++) { for(int j=59;j>=i;j--) { int k; for(k=i;k<=j;k++) str2[k-i]=node[1].s[k]; str2[k]='\0'; memset(next_,-1,sizeof(next_)); len2=j-i+1; get_next(); int ans=1; for(int i=1;i<=n;i++) { if(!kmp(i)) ans=0; if(!ans) break; } if(ans) { if(j-i+1>cc) { aa=i; bb=j; cc=j-i+1; } else if(j-i+1==cc) { if(check(i,j,aa,bb)) { aa=i; bb=j; } } } } } if(cc>=3) { for(int i=aa;i<=bb;i++) cout<<node[1].s[i]; cout<<endl; } else cout<<"no significant commonalities"<<endl; } return 0; } |
版权声明:本文为博主原创文章,欢迎指教~
原文地址:http://blog.csdn.net/u013712847/article/details/48010099