Blue Jeans
POJ - 3080
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 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.
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.
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
no significant commonalities
AGATAC
CATCATCAT
题意:
求最大的公共连续子序列
代码:
(KMP)
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char s[15][65],b[65],ans[65];
int nex[65],n,m;
void get_next(char *b){
int m=strlen(b);
int j=-1,i=0;
nex[0]=-1;
while(i<m)
{
if(j==-1 || b[i]==b[j])nex[++i]=++j;
else j=nex[j];
}
}
int KMP(char *a,char *b){
get_next(b);
int n=strlen(a);
int m=strlen(b);
int j=0,i=0;
while(i<n){
if(j==-1 || b[j]==a[i]){
i++;j++;
}
else j=nex[j];
if(j>=m)return 1;
}
return 0;
}
bool find(int len){//判断是否存在公共连续子序列
for(int i=0;i+len-1<60;i++){
int f=1;
strncpy(b,s[0]+i,len);
b[len]='\0';
for(int j=1;j<n;j++){
if(!KMP(s[j],b)){
f=0;
break;
}
}
if(f)return 1;
}
return 0;
}
bool solve(int len){
int k=1;
for(int i=0;i+len-1<60;i++){
strncpy(b,s[0]+i,len);
b[len]='\0';
int f=1;
for(int j=1;j<n;j++){
if(!KMP(s[j],b)){
f=0;
break;
}
}
if(f){
if(k){//建立比较基数
strncpy(ans,b,len+1);
k=0;
}else if(strcmp(ans,b)>0)
strncpy(ans,b,len+1);
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int f=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",s[i]);
int maxn=0;
for(int i=3;i<=60;i++){
if(find(i)){//找到了长度为 i 的连续子序列
maxn=max(maxn,i);//记录最大长度
f=1;//标记存在
}
}
if(f)
solve(maxn);//找到最大的公共连续子序列
if(f) printf("%s\n",ans);
else printf("no significant commonalities\n");
}
return 0;
}
(字符串解题)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,ok=0;
string s[15];
scanf("%d",&n);
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=60;i>=3;i--){//串的长度
int w=0;//满足此长度的子序列数量
string num[65];
for(int j=0;j+i-1<60;j++){
string b=s[0].substr(j,i);//找到s[0]某一子序列
int f=1;
for(int k=1;k<n;k++){
if(s[k].find(b) == string::npos){//无此子序列
f=0;
break;
}
}
if(f){
num[w++]=b;
}
}
if(w>0){
sort(num,num+w);
cout<< num[0] <<endl;
ok=1;
break;
}
}
if(!ok)printf("no significant commonalities\n");
}
return 0;
}