您的位置:首页 > 产品设计 > UI/UE

POJ3038(Blue Jeans)

2015-08-17 19:27 501 查看
Blue Jeans

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 14301Accepted: 6366
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 (2 <= m <= 10) 个长60的字符串,求最长公共连续子串。

思路:对第一个串枚举起点和终点,分别得到连续的子串,然后分别和所有的剩下的m-1个串进行匹配,找到长度最大的,字典序靠前的连续子串。匹配用的是kmp算法。

#include<cstdio>
#include<string.h>
int t,n;
char a[12][70];
char T[70],S[70];
int len, lenT;
char ans[70];
int nxt[70];
void getnxt(){
int j = 0,k=-1;
memset(nxt,0,sizeof(nxt));
nxt[0]=-1;
while(j<len){
if(k==-1||T[j]==T[k])
nxt[++j]=++k;
else
k=nxt[k];
}
}
void kmp() {
if(lenT<len)
return;
getnxt();
int tag=1;
for(int m = 1; m < n; m++) {
int j=0,k=0;
while(k<lenT&&j<60) {
if(k==-1||T[k]==a[m][j])
++k,++j;
else
k=nxt[k];
}
if(k!=lenT)tag=0;
}
if(tag&&lenT>=len) {
if(lenT==len) {
if(strcmp(ans,T)>0)
for(int i = 0; i < len; i++)ans[i]=T[i];
} else
for(int i = 0; i <lenT; i++)ans[i]=T[i],len=lenT;
}
}
int main(){
scanf("%d",&t);
while(t--){
len=0;
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%s",a[i]);
for(int i = 0; i < 58; i++){
for(int j = i+2; j < 60; j++){
for(int k = 0; k+i<= j; k++)T[k]=a[0][i+k];
lenT=j-i+1;
kmp();
}
}
if(len==0)
printf("no significant commonalities\n");
for(int i = 0; i < len; i++)
if(i==len-1)printf("%c\n",ans[i]);
else printf("%c",ans[i]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: