zoukankan      html  css  js  c++  java
  • 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;
        } 
    }
  • 相关阅读:
    C++虚函数表理解
    【转】c++虚函数实现原理
    【转】运输层TCP协议详细介绍
    【转】计算机中浮点数的表示
    ARP跨网段广播
    【转】arm和x86的区别
    面向对象六大原则
    安卓 热修复的原理
    Zygote和System进程的启动过程
    Android内存泄漏
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12689640.html
Copyright © 2011-2022 走看看