POJ

来源:互联网 发布:java代码混淆工具 编辑:程序博客网 时间:2024/05/29 08:05
Blue Jeans
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18660 Accepted: 8298

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

32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalitiesAGATACCATCATCAT
求所给多个字符串,最长的 字典序最小的 长度大于等于3的 相同的 子串
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;#define min(a,b) (a>b?b:a)char a[11][65],b[65];int kmp_next[65];void KMP_next(){    int i=0,j=-1;    kmp_next[0]=-1;    while(b[i])        if(j==-1||b[i]==b[j])            kmp_next[++i]=++j;        else            j=kmp_next[j];}bool KMP(int k){    int i=0,j=0,m=strlen(b);    while(a[k][i])    {        if(j==-1||a[k][i]==b[j])            ++i,++j;        else            j=kmp_next[j];        if(j==m)            return true;    }    return false;}int main(){    int N,n;    cin>>N;    while(N--)    {        cin>>n;        int vju=1,flag=200000;        for(int i=0; i<n; i++)        {            scanf("%s",a[i]);            flag=min(flag,strlen(a[i]));        }        char ans[208]= {""};        for(; flag>=3&&vju; flag--) //子串长度        {            for(int i=0; i<=60-flag; i++) //选取子串            {                strcpy(b,a[0]+i);                b[flag]=0;                KMP_next();                int j;                for(j=1; j<n; j++)                    if(!KMP(j))                        break;                if(j==n)                {                    if(vju||strcmp(b,ans)<0)                        strcpy(ans,b);                    vju=0;                }            }            if(!vju)                break;        }        if(strlen(ans))            cout<<ans<<endl;        else            cout<<"no significant commonalities"<<endl;    }    return 0;}


原创粉丝点击