Language:
Blue Jeans
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:
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 Source |
暴力本可以过的。。。不过刚学的KMP 就拿KMP练练手 使用了string型容易copy字符串。
#include <stdio.h>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std;
char St[100][100];
int pre[100];
int n;
string res;
bool bb;
void getnext(string P,int plen)
{
pre[0]=pre[1]=0;
for(int i=1;i<plen;i++)
{
int k=pre[i];
while(k&&P[i]!=P[k]) k=pre[k];
pre[i+1] = P[i]==P[k]? k+1 : 0;
}
}
int search(char *T,string P)
{
int slen=strlen(T);
int plen=P.length();
getnext(P,plen);
int k=0;
for(int i=0;i<slen;i++)
{
while(k&&P[k]!=T[i]) k=pre[k];
if(P[k]==T[i]) k++;
if(k==plen) return 1;
}
return 0;
}
void solve(int n)
{
bb=true;
string tmp;
string ch=St[0];
for(int i=60;i>=3;i--)
{
for(int j=0;j+i<=60;j++)
{
int flag=1;
tmp= ch.substr(j,i);
for(int k=0;k<n;k++)
{
if(search(St[k],tmp)==0) //注意此处是不匹配的时候记录
{
flag=0;
break;
}
}
if(flag)
{
if(bb) res=tmp,bb=false;
if(tmp<res) res=tmp;
}
}
if(!bb) return;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s",St[i]);
solve(n);
if(!bb) cout<<res<<endl;
else printf("no significant commonalities\n");
}
return 0;
}