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.
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
题意: 求出几条字符串中相同的子串
思路: KMP+枚举(第一条字符串再与剩下的字符串匹配)
CODE:
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
char t[15][70];
int next[70],k;
void getnext(char *a)
{
int i=0,j=-1;
next[0]=-1;
while(i<k)
{
if(j==-1||a[i]==a[j])
{
++i;++j;
if(a[i]!=a[j]) next[i]=j;
else next[i]=next[j];
}
else j=next[j];
}
}
int kmp(int ii,char *a)
{
int i=0,j=0;
while(i<60)
{
if(j==-1||t[ii][i]==a[j])
{
++i;++j;
}
else j=next[j];
if(j==k) return 1;
}
return 0;
}
int main()
{
freopen("poj3080.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
int ok=0;
char ans[70];
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",t[i]);
}
k=60;
char a[70];
while(k>=3)
{
memset(ans,'\0',sizeof(ans));
for(int i=0;i<=60-k;i++)
{
memset(a,'\0',sizeof(a));
strncpy(a,t[0]+i,k); //子串复制到a上
// printf("%s\n",a);
int c=0;
getnext(a);
for(int j=1;j<n;j++) //遍历所有剩下的字符串
{
if(kmp(j,a)==1)
{
c++; //printf("%s\n",a);
if(c==n-1)
{
ok=1;
if(ans[0]=='\0')
{
strcpy(ans,a);
// printf("%s\n",ans);
}
else if(k==strlen(ans)&&strcmp(ans,a)>0)
{
strcpy(ans,a);
}
}
}
else break;
}
}
if(ans[0]!='\0') break;
else k--;
}
if(ok==1) printf("%s\n",ans);
else printf("no significant commonalities\n");
}
return 0;
}