Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16546 Accepted: 7350
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
题目的意思大概是多串DNA序列组合,然后你求出其中连续的组合,如果连续数小于3就输出no significant commonalities,否则输出该序列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
#define mod 9901
using namespace std;
char s[15][65],ch[65];
int next[105],n,ans,len;
void set_next()
{
int i,j;
next[0]=-1;
j=-1;
i=0;
while(i<len)
{
if(j==-1||ch[j]==ch[i])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
void KMP()
{
int i,j,k,m;
set_next();
ans=100;
for(k=1; k<n; k++)
{
i=0,j=0,m=0;
while(i<60&&j<len)
{
if(j==-1||s[k][i]==ch[j])
{
i++;
j++;
}
else
{
j=next[j];
}
if(j>m) m=j;
}
if(m<ans) ans=m;
}
}
int main()
{
int T;
scanf("%d",&T);
int i,j;
char c[65];
while(T--)
{
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%s",s[i]);
int w=0;
for(i=0; i<60; i++)
{
strcpy(ch,s[0]+i);
len=60-i;
KMP();
if(w<ans)
{
w=ans;
strncpy(c,s[0]+i,ans);
c[ans]='\0';
}
if(w==ans)
{
char t[65];
strncpy(t,s[0]+i,ans);
t[ans]='\0';
if(strcmp(t,c)<0)
strcpy(c,t);
}
}
if(w<3)
printf("no significant commonalities\n");
else
printf("%s\n",c);
}
return 0;
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
char str[15][100];
int n, next[100];
string res;
bool flag;
void GetNxt( string T, int *nxt, int len ){
int i = 0, j = 1;
while( j <= len ){
if( i == 0 || T[i-1] == T[j-1] )
nxt[++j] = ++i;
else i = nxt[i];
}
}
bool kmp(char *S, string T){
int la = strlen(S), lb = T.size();
int i = 1, j = 1;
GetNxt( T, next, lb );
while( i <= la && j <= lb ){
if( j == 0 || S[i-1] == T[j-1] ) i++, j++;
else j = next[j];
if( j > lb ) return true;
}
return false;
}
void solve(){
flag = false;
string st = str[0], tmp;
for(int L = 60; L >= 3; L--){
for(int i = 0; i+L <= 60; i++){
tmp = st.substr(i,L);
bool a = true;
for(int k = 1; k < n && a; k++)
if( kmp( str[k], tmp ) == false ) a = false;
if( a == true ){
if( flag == false ) flag = true, res = tmp;
if( res > tmp ) res = tmp;
}
}
if( flag ) return;
}
}
int main(){
int T;
scanf("%d", &T);
while( T-- ){
scanf("%d", &n );
for(int i = 0; i < n; i++)
scanf("%s", str[i] );
solve();
if( flag == false ) puts("no significant commonalities");
else printf("%s\n", res.c_str() );
}
return 0;
}