POJ3080 Blue Jeans (最长公共子序列 KMP)

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

3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT

 给你n个字符串,要你求出这n个字符串的最长公共连续子序列是哪个,如果存在多个最长的,就输出字典序最小的那个。

只需要枚举第一个串的长度>=3的所有字串,然后用该字串KMP匹配其他剩下n-1个串,看看能否找到匹配点即可。如果长度L符合要求,那么就去找到长度为L且字典序最小的串。

本题枚举字串长度需要二分,找出可行的最长字串长度,然后在当前长度中枚举所有可能串,再找出字典序最小的那个串.

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
char s[15][100];
char P[100];
char ans[100];//最终的结果字符串
int m,next[100];
int n;//表示有多少个主串

void GetNext()
{
    m=strlen(P);
    next[0]=next[1]=0;
    for(int i=1;i<m;i++)
    {
        int j =next[i];
        while(j && P[i]!=P[j]) j=next[j];
        next[i+1] = (P[i]==P[j])?j+1:0;
    }
}

bool Find(char *T)
{
    int j=0;
    GetNext();
    for(int i=0;i<60;i++)
    {
        while(j&&T[i]!=P[j])
            j=next[j];
        if(T[i]==P[j])
        j++;
        if(j==m)
        return true;
    }
    return false;
}
bool solve_1(int len)//判断第0个base串的长为len的字串中是否有可行串
{
    for(int i=0;i+len-1<60;i++)
    {
        strncpy(P,s[0]+i,len);
        P[len]=0;//P串末尾加'\0'
        bool ok=true;
        for(int j=1;j<n;j++)
        if(!Find(s[j]))
        {
            ok=false;
            break;
        }
        if(ok)
            return true;
    }
    return false;
}
void solve_3(int len)//找出长度为len的可行字串中字典序最小的,放在ans中
{
    bool first=true;
    for(int i=0;i+len-1<60;i++)
    {
        strncpy(P,s[0]+i,len);
        P[len]=0;//P串末尾加'\0'
        bool ok=true;
        for(int j=1;j<n;j++)
        if(!Find(s[j]))
        {
            ok=false;
            break;
        }
        if(ok)
        {
            if(first)
            {
                strncpy(ans,P,len+1);
                first=false;
            }
            else if(strcmp(ans,P)>0)//字典序小才更新
                strncpy(ans,P,len+1);
        }
    }
}
bool solve()
{
    int L=3,R=60;
    if(!solve_1(3))
        return false;
    while(R>L)
    {
        int m=L+(R-L+1)/2;
        if(solve_1(m))
            L=m;
        else
            R=m-1;
    }
    //找到了可行字串的最长长度为L,然后需要找出字典序最小的
    solve_3(L);//找到长L的字典序最小的字串存在ans中
    return true;
}

int main()
{
    int kase;
    scanf("%d",&kase);
    while(kase--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        if(!solve())//最终结果存在ans中
            printf("no significant commonalities\n");
        else
            printf("%s\n",ans);

    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值