zoukankan      html  css  js  c++  java
  • HDU 1560 DNA sequence (迭代加深搜索)

    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. 

    InputThe 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.OutputFor 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

    思路
    如果DNA最长的串长度为n,那就先搜索以n为长度,是否存在符合条件的母串,若不存在,再搜索n+1;
    这便是所谓的迭代加深搜索。结束。
    代码中用到的maxx,只是一个剪枝而已。
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1e9+7;
    const double eps = 1e-6;
    
    char s[12][12];
    int maxs=0;
    int tot[12];
    int n,pos[12];
    char a[5]="ACGT";
    
    void view()
    {
        for(int i=1;i<=n;i++){
            cout<<pos[i]<<" ";
        }
        cout<<endl;
    }
    
    bool dfs(int t)
    {
    
        int maxx=0;
        for(int i=1;i<=n;i++){
            maxx=max(maxx,tot[i]-pos[i]);
        }
        if(maxx==0){return true;}
        if(t+maxx>maxs){return false;}
        bool vis[12];
        for(int i=0;i<4;i++){
            memset(vis,0,sizeof(vis));
            for(int j=1;j<=n;j++){
                if(s[j][pos[j]]==a[i]){
                    pos[j]++;
                    vis[j]=true;
                }
            }
            if(dfs(t+1)){return true;}
            for(int j=1;j<=n;j++){
                if(vis[j]){
                    pos[j]--;
                }
            }
        }
        return false;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&n);
            maxs=0;
            for(int i=1;i<=n;i++){
                scanf("%s",s[i]);
                tot[i]=strlen(s[i]);
                maxs=max(maxs,tot[i]);
            }
    
            while(true){
                memset(pos,0,sizeof(pos));
                if(dfs(0)){
                    printf("%d
    ",maxs);
                    break;
                }
                else maxs++;
            }
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    带你玩转Flink流批一体分布式实时处理引擎
    都2022年了,你的前端工具集应该有vueuse
    云图说|图解DGC:基于华为智能数据湖解决方案的一体化数据治理平台
    面试官: Flink双流JOIN了解吗? 简单说说其实现原理
    4种Spring Boot 实现通用 Auth 认证方式
    这8个JS 新功能,你应该去尝试一下
    Scrum Master需要具备哪些能力和经验
    dart系列之:时间你慢点走,我要在dart中抓住你
    dart系列之:数学什么的就是小意思,看我dart如何玩转它
    dart系列之:还在为编码解码而烦恼吗?用dart试试
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/9614638.html
Copyright © 2011-2022 走看看