码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1560 DNA sequence(IDA*)

时间:2020-04-13 10:35:32      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:example   seq   tput   c++   otto   number   ssi   mem   tween   

DNA sequence

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6042    Accepted Submission(s): 2735


Problem Description
The twenty-first century is a biology-technology developing century. We know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Finding the longest common subsequence between DNA/Protein sequences is one of the basic problems in modern computational molecular biology. But this problem is a little different. Given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given sequence is the subsequence of it.

For example, given "ACGT","ATGC","CGTT" and "CAGT", you can make a sequence in the following way. It is the shortest but may be not the only one.

技术图片
 
Input
The first line is the test case number t. Then t test cases follow. In each case, the first line is an integer n ( 1<=n<=8 ) represents number of the DNA sequences. The following k lines contain the k sequences, one per line. Assuming that the length of any sequence is between 1 and 5.
 
Output
For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.
 
Sample Input
1 4 ACGT ATGC CGTT CAGT
 
Sample Output
8
 

题目大意与分析

给出k个以A C G T为元素的字符串,求以他们为子串的字符串的最短长度

IDA*,枚举ACGT,如果匹配,就进入下一层

#include<bits/stdc++.h>

using namespace std;

string a[10];
int cnt[10],t,k,minl;
char s[5]="ACGT";
bool dfs(int now,int step)
{
    int flag=1;
    for(int i=0;i<k;i++)
    {
        if(cnt[i]!=a[i].size())
        {
            flag=0;
        }
        if(a[i].size()-cnt[i]>(step-now+1))   //剪枝,所剩的长度过长 
        return false;
    }
    if(flag)
    return true;
    if(now>step)
    return false;
    for(int i=0;i<4;i++)
    {
        int next=0;
        int temp[10];
        for(int j=0;j<k;j++)
        {
            temp[j]=cnt[j];
            if(cnt[j]!=a[j].size())
            {
                if(a[j][cnt[j]]==s[i])
                {
                    cnt[j]++;
                    next=1;
                }
            }
        }
        if(next)                           //如果可以匹配,进入下一层 
        {
            if(dfs(now+1,step))
            return true;
            for(int j=0;j<k;j++)
            {
                cnt[j]=temp[j];            //回溯 
            }
        }
    }
    return false;
}

int main()
{
    cin>>t;
    while(t--)
    {
        minl=0;
        cin>>k;
        for(int i=0;i<k;i++)
        {
            cin>>a[i];
            int l=a[i].size();
            minl=max(minl,l);
        }
        int anss;
        memset(cnt,0,sizeof(cnt));
        for(int i=minl;;i++)
        {
            if(dfs(1,i))
            {
                anss=i;
                break;
            }
        }
        cout<<anss<<endl;
    } 
}

 

HDU 1560 DNA sequence(IDA*)

标签:example   seq   tput   c++   otto   number   ssi   mem   tween   

原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12689640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!