Loading

HDU 1560 DNA sequence(IDA*)

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;
    } 
}

 

posted @ 2020-04-13 10:08  dyhaohaoxuexi  阅读(268)  评论(0编辑  收藏  举报