POJ 3080 Blue Jeans (分解字符串,KMP)

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

给你n个字符串求出他们的最长相同部分,注意每个字符串的长度可能不相同,坑爹样例

求出第一个字符串的所有子串(利用长度来去分解),然后再kmp求匹配后面的所有字符串,如果可以匹配,就更新一下字符串,在长度相等时,优先取字典序小的字符串

代码如下

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;
const int MAX = 65;
char str[15][MAX];
int fail[MAX];
void Getfail(char s[],int n){
    int i = 0,k = -1;
    fail[0] = -1;
    while(i < n){
        if(k == -1 || s[i] == s[k])
            fail[++i] = ++k;
        else
            k = fail[k];
    }
}

bool kmp(char s1[],char s2[]){
    int len1 = strlen(s1),len2 = strlen(s2);
    int i = 0,j = 0;
    while(i < len1){
        if(j == -1 || s1[i] == s2[j])
            i++,j++;
        else
            j = fail[j];
        if(j == len2)
            return true;
    }
    return false;
}
int main(void){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s",str[i]);
        int len_first = strlen(str[0]);
        char ans[MAX] = {0},tem[MAX];
        bool isfind = false;
        for(int len = 60;len>=3;len--){//利用长度来分解字符串
            for(int i=0;i<=len_first-len;i++){//从第i个位置拷贝len个字符到tem里面,即分解子串
                strncpy(tem,str[0]+i,len);
                tem[len] = '\0';
      //          printf("%d %d %s\n",len,i,tem);
                Getfail(tem,len);
                bool isok = true;//判断是不是1-n所有的字符串都能匹配这个子串
                for(int i=1;i<n;i++){
                   if(!kmp(str[i],tem)) isok = false;
                }
                if(isok){//更新答案
                    if(len > strlen(ans)){
                        isfind = true;
                        strcpy(ans,tem);
                    }
                    else if(len == strlen(ans) && strcmp(ans,tem) == 1){
                        isfind = true;
                        strcpy(ans,tem);
                    }
                }
            }
        }
        if(isfind)  printf("%s\n",ans);
        else    printf("no significant commonalities\n");//没有发现这样的子串
    }

    return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页