題目鏈接:http://poj.org/problem?id=3080點擊打開鏈接
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19455 | Accepted: 8657 |
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
Source
將其中一個字符串枚舉每個子串用kmp判斷
#include <string>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string.h>
using namespace std;
int nnext[111];
char ss[11][111];
char mmid[111];
void getnext()
{
int i=0;
int j=-1;
nnext[0]=-1;
int len=strlen(mmid);
while(i<len)
{
if(j==-1||mmid[i]==mmid[j])
{
i++;
j++;
nnext[i]=j;
}
else
j=nnext[j];
}
}
int kmp(int s)
{
int i=0;int j=0;
int len=strlen(ss[s]);
int lenn=strlen(mmid);
getnext();
while(i<len)
{
if(j==-1||ss[s][i]==mmid[j])
{
i++;
j++;
}
else
j=nnext[j];
if(j==lenn)
return 1;
}
return 0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
char ans[111]="";
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf(" %s",ss[i]);
}
int len=strlen(ss[0]);
for(int i=0;i<len;i++)
for(int j=i+1;j<len;j++)
{
mmid[0]='\0';
int cnt=0;
for(int k=i;k<=j;k++)
mmid[cnt++]=ss[0][k];
mmid[cnt]='\0';
int flag=1;
//printf("%s\n",mmid);
//getchar();
for(int k=1;k<n;k++)
flag&=kmp(k);
if(flag)
{
if(strlen(ans)<strlen(mmid))
{
for(int ii=0;ii<strlen(mmid);ii++)
{
ans[ii]=mmid[ii];
}
}
else if(strlen(ans)==strlen(mmid))
{
if(strcmp(ans,mmid)>=1)
{
for(int ii=0;ii<strlen(mmid);ii++)
{
ans[ii]=mmid[ii];
}
}
}
}
}
if(strcmp(ans,"")==0)
printf("no significant commonalities\n");
else
printf("%s\n",ans);
}
}