POJ3080Blue Jeans

本文介绍了一种用于比对多条DNA序列并找出最长公共子序列的算法实现。该算法适用于基因研究,如《国家地理》与IBM合作的《基因图谱计划》,通过分析DNA贡献者的信息来绘制地球人口分布图。文章提供了两种不同的实现方式,包括KMP算法的应用。
摘要由CSDN通过智能技术生成

Blue Jeans
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 16546 Accepted: 7350
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
题目的意思大概是多串DNA序列组合,然后你求出其中连续的组合,如果连续数小于3就输出no significant commonalities,否则输出该序列

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
#define mod 9901
using namespace std;
char s[15][65],ch[65];
int next[105],n,ans,len;

void set_next()
{
    int i,j;
    next[0]=-1;
    j=-1;
    i=0;
    while(i<len)
    {
        if(j==-1||ch[j]==ch[i])
        {
            i++;
            j++;
            next[i]=j;
        }
        else
            j=next[j];
    }
}

void KMP()
{
    int i,j,k,m;
    set_next();
    ans=100;
    for(k=1; k<n; k++)
    {
        i=0,j=0,m=0;
        while(i<60&&j<len)
        {
            if(j==-1||s[k][i]==ch[j])
            {
                i++;
                j++;
            }
            else
            {
                j=next[j];
            }
            if(j>m) m=j;
        }
        if(m<ans) ans=m;
    }

}

int main()
{
    int T;
    scanf("%d",&T);
    int i,j;
    char c[65];
    while(T--)
    {
        scanf("%d",&n);
        for(i=0; i<n; i++)
            scanf("%s",s[i]);
        int w=0;
        for(i=0; i<60; i++)
        {
            strcpy(ch,s[0]+i);
            len=60-i;
            KMP();
            if(w<ans)
            {
                w=ans;
                strncpy(c,s[0]+i,ans);
                c[ans]='\0';
            }
            if(w==ans)
            {
                char t[65];
                strncpy(t,s[0]+i,ans);
                t[ans]='\0';
                if(strcmp(t,c)<0)
                strcpy(c,t);
            }
        }
        if(w<3)
        printf("no significant commonalities\n");
        else
        printf("%s\n",c);
    }
    return 0;
}
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;

char str[15][100];
int n, next[100];
string res;
bool flag;

void GetNxt( string T, int *nxt, int len ){
    int i = 0, j = 1;
    while( j <= len ){
        if( i == 0 || T[i-1] == T[j-1] ) 
            nxt[++j] = ++i;
        else    i = nxt[i];
    }
}
bool kmp(char *S, string T){
    int la = strlen(S), lb = T.size();
    int i = 1, j = 1;
    GetNxt( T, next, lb );
    while( i <= la && j <= lb ){
        if( j == 0 || S[i-1] == T[j-1] ) i++, j++;
        else j = next[j];
        if( j > lb ) return true;
    }
    return false;
}
void solve(){
    flag = false;    
    string st = str[0], tmp;    
    for(int L = 60; L >= 3; L--){
        for(int i = 0; i+L <= 60; i++){    
            tmp = st.substr(i,L);
            bool a = true;    
            for(int k = 1; k < n && a; k++)
                if(  kmp( str[k], tmp ) == false ) a = false;
            if( a == true ){
                if( flag == false ) flag = true, res = tmp;
                if( res > tmp ) res = tmp;
            }    
        }
        if( flag ) return;    
    }
}
int main(){
    int T;    
    scanf("%d", &T);
    while( T-- ){
        scanf("%d", &n );
        for(int i = 0; i < n; i++)
            scanf("%s", str[i] );
        solve();
        if( flag == false ) puts("no significant commonalities");
        else printf("%s\n", res.c_str() );
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值