hdu 1560 DNA sequence(迭代加深搜索)

题意:找到一个最短的序列,使得输入的所有序列都是这个序列的子序列。输出最短序列的长度


DNA sequence

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


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
 



思路:dfs枚举所有情况,由于深度未知,所以可以利用迭代加深搜索的方式,。

因为最理想的序列也不可能比任意一条序列短,所以每次搜索只需搜到 当前长度+剩下未使用的最长长度=deep就够了。


迭代加深搜索适用于搜索深度未知,bfs又因为内存不够,无法使用的时候。

具体做法就是在每次dfs时规定一个deep,当搜索层数超过了这个deep时,就退出,然后deep++。


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char C[]={"ATCG"};
struct node
{
    char dna[6];
    int length;
}a[10];
int n,t,deep;
int pos[10];        //记录当前序列使用的位置

int calmax()
{
    int maxs=0;
    for(int i=1;i<=n;i++)
        maxs=max(a[i].length-pos[i],maxs);
    return maxs;
}
bool dfs(int k)
{
    int maxs=calmax();
    if(k+maxs>deep) return false; //注意这里,k为当前长度,maxs为当前还没使用的最长的长度
                                  //显然k+maxs是当前最理想的长度,如果它>deep,就不用继续搜了
    if(maxs==0) return true;
    for(int i=0;i<4;i++)
    {
        int flag=0;

        int tmp[10];
        for(int j=1;j<=n;j++)
        tmp[j]=pos[j];              //保存信息,出口处还原

        for(int j=1;j<=n;j++)
        {
            if(a[j].dna[ pos[j] ]==C[i])
            {
                flag=1;
                pos[j]++;
            }
        }
        if(flag)
        {
            if(dfs(k+1)) return true;
            else
            {
                for(int j=1;j<=n;j++)   //还原
                    pos[j]=tmp[j];
            }
        }
    }
    return false;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        int maxs=-1;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a[i].dna);
            a[i].length=strlen(a[i].dna);
            pos[i]=0;
            maxs=max(maxs,a[i].length);
        }
        deep=maxs;
        while(1)
        {
            if(dfs(0)) break;
            deep++;
        }
        printf("%d\n",deep);
    }
    return 0;
}



  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TommyTT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值