HDU 1560 DNA sequence(初学IDA*)

16 篇文章 0 订阅

题面

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

参考链接

HDU1560:DNA sequence(IDA星)

A*算法 和 IDA*算法

题意简述

求包含题目所给n个序列的序列的最短长度

用BFS做这道题,妥妥地MLE了

初学IDA*

get_h()计算估价函数,即时退出

程序

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int n,deep;/**deep即长度**/
char c[]="ACGT";
struct Node
{
    char s[10];
    int len;
}node[10];
int where[10];/**第i个序列已经匹配where[i]个**/


int get_h()/**估价函数|此时还需的最大长度**/
{
    int answer=0;
    for(int i=1;i<=n;i++)
        answer=max(answer,node[i].len-where[i]);
    return answer;
}

bool dfs(int step)/**已有长度**/
{
    if(step+get_h()>deep)
        return false;
    if(get_h()==0)/**匹配完成**/
        return true;
    int temp[10];/**临时保存where数组**/
    for(int i=0;i<4;i++)
    {
        bool flag=false;
        for(int j=1;j<=n;j++)
            temp[j]=where[j];/**把where保存下来**/
        for(int j=1;j<=n;j++)
        {
            if(node[j].s[where[j]]==c[i])
            {
                flag=true;
                where[j]++;
            }
        }
        if(flag)
        {
            if(dfs(step+1))
                return true;
            for(int j=1;j<=n;j++)
                where[j]=temp[j];
        }
    }
    return false;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int max_len=0;
        memset(where,0,sizeof(where));
        for(int i=1;i<=n;i++)
        {
            scanf("%s",node[i].s);
            node[i].len=strlen(node[i].s);
            max_len=max(max_len,node[i].len);
        }
        deep=max_len;
        while(1)
        {
            if(dfs(0))
                break;
            deep++;
        }
        printf("%d\n",deep);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值