POJ 3080 Blue Jeans (KMP+暴力列舉)【模板】

wust_zwl發表於2017-08-16

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

【題解】

 題意:給定長度為60的m個字串,找它的最長公共子串,如果長度相同,輸出字典序小的,如果找到的公共子串小於3 ,就輸出 一串不認識的字母,否則就輸出找到的那一串同樣不認識的字母。


 分析:

 注意到,這個題給的資料範圍很小,m不大於10,長度不大於60,吐過暴力列舉的話,複雜度大概是60*60*10*(60+60),不會爆,所以果斷列舉,詳細步奏見程式碼註釋;

【AC程式碼】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=65;
int m,n;
char s[11][61]; //記錄輸入的串
char str[61];
char ans[66];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&m);
        for(int i=1;i<=m;++i)
            scanf("%s",s[i]);
        memset(ans,'\0',sizeof(ans));//注意這裡初始化,不然後面第一次strlen時會出錯
        for(int j=1;j<=60;++j)//子串長度
        {
            int cnt=0; //長度為j的公共串是否找到
            for(int k=0;k<=60-j;++k)//遍歷長度為j的子串
            {
                int flag=1;//標記剩下m-1個串中是否含有長度為j的子串
                int w=0;
                int ss=k;
                while(ss<j+k)
                    str[w++]=s[1][ss++];
                str[w]='\0'; //這裡也注意 後面要用到strlen,所以必須以\0結尾
                for(int i=2;i<=m;++i)//遍歷剩下的m-1個串
                {
                    if(!strstr(s[i],str)) //查詢函式  如果str[i]母串中含有str子串返回其下標值  否則返回NULL
                    {
                        flag=0;//只要m-1個串中有一個不含有str子串  就直接跳出
                        break;
                    }
                }
                if(flag) //都存在str子串
                {
                    cnt=1; //標記長度為j的公共子串是否存在
                    if(strlen(str)>strlen(ans)) //長度優先
                        strcpy(ans,str);
                    else if(strcmp(str,ans)<0)//字典序小的優先
                        strcpy(ans,str);
                }
            }
            if(!cnt) break; //注意這裡  很重要的剪枝  如果不存在長為j的公共子串  就更不可能存在長度>J的子串 所以直接跳出
        }
        if(strlen(ans)<3) //題目要求
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);
    }
    return 0;
}



相關文章