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
以第一个从串为模板,二重for循环枚举第一个串的子串,然后以这个子串没模板串,用kmp匹配其他的串,当剩下的串中都包含有这个子串时,选取最长且字典序最小的串
code:
代码:
//Full of love and hope for life
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/
using namespace std;
char n[100],m[100];
int nex[100];
char s[20][100];
/*
void getnext(int a[],int x){
int j=-1;
nex[0]=-1;
for(int i=1;i<x;i++){
if(j!=-1&&a[i]!=a[j+1]){
j=nex[j];
}
if(a[i]==a[j+1]){
j++;
}
nex[i]=j;
}
}
*/
void getnext(char a[],int x){
memset(nex,0,sizeof(nex));
int j=-1;
nex[0]=-1;
int i=0;
while(i<x){
if(j==-1||a[j]==a[i]){
i++;
j++;
nex[i]=j;
}
else{
j=nex[j];
}
}
}
bool kmp(char a[],char b[],int x,int y){
getnext(a,x);
int j=0;
int i=0;
while(i<y){
if(j==-1||a[j]==b[i]){
i++;
j++;
}
else{
j=nex[j];
}
if(j>=x){
return true;
}
}
return false;
}
/*
int kmp(char a[],char b[],int x,int y){
getnext(b,y);
int j=0;
for(int i=0;i<x;i++){
while(j!=-1&&a[i]!=b[j+1]){
j=nex[j];
}
if(a[i]==b[j+1]){
j++;
}
if(j==y-1){
ans++;
}
}
return ans;
}
*/
int main(){
//ios::sync_with_stdio(false);
int a,b,k;
cin >> a;
while(a--){
bool flag=false;
cin >> b;
for(int i=0;i<b;i++){
cin >> s[i];
}
int len=strlen(s[0]);
for(int i=0;i<len;i++){
int t=0;
m[t++]=s[0][i];
for(int j=i+1;j<len;j++){
m[t++]=s[0][j];
for(k=1;k<b;k++){
if(!kmp(m,s[k],t,strlen(s[k]))){
break;
}
}
if(k>=b){
if(!flag||t>strlen(n)){
m[t]='\0';
strcpy(n,m);
flag=true;
}
else{
m[t]='\0';
if(t==strlen(n)&&strcmp(m,n)<0){
strcpy(n,m);
}
}
}
}
}
if(!flag||strlen(n)<3){
cout << "no significant commonalities" << endl;
continue;
}
cout << n << endl;
}
return 0;
}
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
string a[1000];
int main()
{
int t,i,n,j,k;
cin>>t;
while(t--)
{
cin>>n;
string b="";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<=60;i++)//此处60为题目要求长度
{
for(j=0;j<=60;j++)
{
int p=0;
string s=a[0].substr(j,i); //截取下标为j后I位的子串
for(k=1;k<n;k++)
{
if(a[k].find(s)==string::npos)
{
p=1;
break;
}
}//枚举各串,判断该子串是否为公共子串
if(p==0)
if(s.size()>b.size()) b=s;
else if(s.size()==b.size()&&b>s)b=s;
}//若为公共子串,进行最长更新
}
if(b.size()<3)
printf("no significant commonalities\n");
else
cout<<b<<endl;
}
return 0;
}