POJ3080 Blue Jeans

来源:互联网 发布:非农数据分析 编辑:程序博客网 时间:2024/06/05 22:49

题目链接:POJ3080

Blue Jeans
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 15726 Accepted: 6998

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

Source

South Central USA 2006

[Submit]   [Go Back]   [Status]   [Discuss]


题意:给出一堆串,求出其中公共的子串,如果子串长度小于3的话就输出那一行字。
题目分析:貌似直接暴力枚举子串就可以做出来,写的时候困的要死,所以代码写的很烂,不过也懒得改了,暴力枚举子串+KMP,自动筛选长度大于等于3的子串,注意题目要求如果有相同长度需要给出字典序列最小的,所以遇到相同长度时需要进一步比较。
////  main.cpp//  POJ3080////  Created by teddywang on 16/4/30.//  Copyright © 2016年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[100][100];int nexts[100];char t[100];int n,m;char buf1[100],buf2[100],buf[100],final[100];void getnext(char *s){    int len=strlen(s);    int i=0,j=-1;    nexts[0]=-1;    while(i<len)    {        if(j==-1||s[i]==s[j])        {            if(s[++i]==s[++j])                nexts[i]=nexts[j];            else nexts[i]=j;        }        else j=nexts[j];    }}int kmp(char *s,char *t){    int i=0,j=0;    int len1=strlen(s),len2=strlen(t);    while(i<len1&&j<len2)    {        if(j==-1||s[i]==t[j])        {            ++i;++j;        }        else j=nexts[j];        if(j==len2)            return 1;    }    return 0;}int main(){    cin>>n;    while(n--)    {        memset(s,0,sizeof(s));        cin>>m;        for(int i=0;i<m;i++)            scanf("%s",s[i]);        memset(buf1,0,sizeof(buf1));        memset(buf2,0,sizeof(buf2));        memset(buf,0,sizeof(buf));        memset(final,0,sizeof(final));        int len1=strlen(s[0]);        for(int i=0;i<len1;i++)            buf1[i]=s[0][i];        memset(nexts,0,sizeof(nexts));        int maxlen=0;        int len2;        for(int i=0;i<len1;i++)        {            int len=0;            for(int j=len1-1;j>=i+2;j--)            {                for(int k=0;k<=j-i;k++)                    buf[k]=buf1[k+i];                buf[j-i+1]='\0';                len=j-i+1;               // cout<<len<<endl;                int flag=1;                for(int l=1;l<m;l++)                {                    len2=strlen(s[l]);                    for(int k=0;k<len2;k++)                        buf2[k]=s[l][k];                    buf2[len2]='\0';                    getnext(buf);                    if(kmp(buf2,buf)==0)                    {                        flag=0;                        break;                    }                }                if(flag==1)                {                    if(len>maxlen)                    {                        maxlen=len;                        strcpy(final,buf);                    }                    else if(maxlen==len)                    {                        int flag1=0;                        for(int i=0;i<len;i++)                            if(final[i]>buf[i])                            {                                flag1=1;                                break;                            }                            if(flag1==1) strcpy(final,buf);                    }                    break;                }            }        }        if(strlen(final)>=3)        {            printf("%s\n",final);        }        else printf("no significant commonalities\n");    }}



0 0
原创粉丝点击