| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13225 | Accepted: 5879 |
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
题目大意: 给定一些长度都为60的字符串,找其最长的相同的子串,若子串长度相等,取字典序小的。
解题思路:同poj3450,kmp模式匹配算法。
代码如下:
#include <cstdio>
#include <cstring>
int const maxn=62;
char s[maxn][maxn];
char a[maxn][maxn];
char b[maxn];
int next[maxn];
void get_next(int x,int len)
{
int i=x,j=-1;
next[x]=-1;
while(i<len)
{
if(j==-1)
{
i++;
j=x;
if(s[0][i]==s[0][j])
next[i]=next[j];
else
next[i]=j;
}
else if(s[0][i]==s[0][j])
{
i++;
j++;
next[i]=next[j];
}
else
j=next[j];
}
}
int kmp(int st,int k,int len)
{
int j=st,i=0;
while(i<60&&j<len)
{
if(j==-1)
{
i++;
j=st;
}
else if(s[0][j]==s[k][i])
{
i++;
j++;
}
else
j=next[j];
}
if(j>=len)
return 1;
else
return 0;
}
void change(int st,int len)
{
int cur=0;
for(int i=st;i<st+len;i++)
b[cur++]=s[0][i];
}
int main()
{
int n,m,i,j,k,cnt,st,p,cur;
scanf("%d",&n);
while(n--)
{
p=0;
scanf("%d",&m);
for(i=0;i<m;i++)
{
getchar();
for(j=0;j<60;j++)
scanf("%c",&s[i][j]);
}
for(i=60;i>=3;i--)
{
st=0;
for(k=0;k<60;k++)
b[k]='Z';
while(st+i<=60)
{
memset(next,0,sizeof(next));
get_next(st,st+i);
cnt=0;
for(k=1;k<m;k++)
if(kmp(st,k,st+i))
cnt++;
if(cnt==m-1)
{
cur=0;
for(k=st;k<st+i;k++)
{
if(b[cur++]>s[0][k])
{
change(st,i);
break;
}
}
p=1;
}
st++;
}
if(p)break;
}
if(!p)
printf("no significant commonalities\n");
else
{
for(j=0;j<i;j++)
printf("%c",b[j]);
printf("\n");
}
}
return 0;
}
520




被折叠的 条评论
为什么被折叠?



