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个串(m<=10)求所有串的最长连续公共子串,如有多个解输出字典序最小的题解:按长度倒着暴枚出第一串的所有子串,用kmp查找这个子串是不是在所有其他串中即可但是说好的只有"ACGT"四个字母呢?!说好的只有长度为六十的串呢?!人与人之间基本的信任呢?! 代码如下:
#include<queue> #include<string> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define hi puts("hi!"); using namespace std; int m,n,cnt; string ans,s[15],s1[60]; struct kmp { string ss,sss; int ans,nxt[70]; void getfill() { memset(nxt,0,sizeof(nxt)); for(int j=1;j<sss.size();j++) { int k=nxt[j]; while(k&&sss[j]!=sss[k]) { k=nxt[k]; } nxt[j+1]=(sss[j]==sss[k])?k+1:0; } } void find() { ans=0; getfill(); int k=0; for(int j=0;j<ss.size();j++) { while(k&&ss[j]!=sss[k]) { k=nxt[k]; } if(ss[j]==sss[k]) { k++; } if(k==sss.size()) { ans=1; } } } }k; int main() { // freopen("1.out","w",stdout); scanf("%d",&m); while(m--) { int flag=1,flag1=0; cnt=0; scanf("%d\n",&n); for(int i=1;i<=n;i++) { cin>>s[i]; } int len=s[1].size(); for(int i=len;i>=1;i--) { if(i<3) { puts("no significant commonalities"); break; } for(int l=0;l<=len-i;l++) { string sub=s[1].substr(l,i); flag=1; for(int w=2;w<=n;w++) { k.ss=s[w]; k.sss=sub; k.find(); flag=min(flag,k.ans); } if(flag) { s1[++cnt]=sub; flag1=1; } } if(flag1) { string ans1=s1[1]; for(int ha=2;ha<=cnt;ha++) { if(ans1.compare(s1[ha])==1) { ans1=s1[ha]; } } cout<<ans1<<endl; break; } } } }
原文地址:https://www.cnblogs.com/stxy-ferryman/p/8461692.html