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
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 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
- 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.
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 AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTSample Output
no significant commonalities AGATAC CATCATCAT
题意:多串字符找最长公共子字符.
思路:用一串来拆开,把它最长子字符拆出,用KMP处理。然后循环用这一串子字符来查找其它字符的最长公共序列。
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
int n,nextt[65],len,M,LM,ansM;
char T[15][65],cpt[65],ans[65],tem[65];
void init_KMP()
{
int i=0,j=-1;
nextt[0]=-1;
while(i<len)
{
if(j==-1||cpt[i]==cpt[j])
{
nextt[++i]=++j;
}
else
j=nextt[j];
}
}
int KMP()
{
init_KMP();
LM=65;
for(int k=0; k<n; k++)
{
M=0;
int i=0,j=0;
while(i<60&&j<len)
{
if(j==-1||cpt[j]==T[k][i])
{
i++;
j++;
}
else
j=nextt[j];
M=max(j,M);
}
LM=min(LM,M);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ansM=0;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%s",T[i]);
}
for(int i=0; i<=57; i++)
{
strcpy(cpt,T[0]+i);
// printf("%s\n",cpt);
len=60-i;
KMP();
if(LM>ansM)
{
ansM=LM;
strncpy(ans,cpt,ansM);
ans[ansM]='\0';
}
else if(LM==ansM)
{
strncpy(tem,cpt,ansM);
tem[ansM]='\0';
if(strcmp(ans,tem))
{
strcpy(ans,tem);
}
}
}
if(ansM<3)
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
}