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. 

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

嗯,开始直接暴力枚举的各个字符串顺序,有点蠢。。。题意理解有点偏。

利用point指针数组来记录状态。

可以枚举长度,枚举4个字符。

然后利用check的剪枝。

对于当前你已经利用的字符个数n,可以实现匹配的状态是point[i]那么,

对于剩余的字符个数sum和每个字符串剩余的字符数,就是一个上面的子问题,我们可以递归解决。

直到所有字符都可以匹配,这样就找到解了。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
const int MAXN = 10;
const int inf = 1e9;
int n,m;
//记录字符串
char s[MAXN][MAXN];
//记录字符串的长度
int sl[MAXN];
//记录字符串当前匹配到哪了
int point[MAXN];
char DNA[]="AGCT";
int check()
{
    int res = 0;
    for(int i = 0; i < n; ++i)res = max(res,sl[i] - point[i]);
    return res;
}
int flag;
void dfs(int sum)
{
    int c = check();
    //如果找到了解
    if(!c)
    {
        flag = 1;
        return;
    }
    //当前不可能符合条件
    if(sum < c)return;
    //保存现场
    int temp[10];
    for(int i = 0; i < n; ++i)temp[i] = point[i];
    //搜索开始
    //表示是否有变化
    bool ok = 0;
    //枚举添加上的字符
    for(int i = 0; i < 4; ++i)
    {
        //更新point的状态
        for(int j = 0; j < n; ++j)
        {
            if(s[j][point[j]] == DNA[i])
            {
                point[j]++;
                ok = 1;
            }
        }
        //如果更新了
        if(ok)
        {
            //继续枚举下一个字符
            dfs(sum-1);
            if(flag)return;
            //恢复现场
            for(int j = 0 ;j < n; ++j)point[j] = temp[j];
        }
    }
}




int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int ans = 0;
        for(int i = 0; i < n; ++i)
        {
            scanf("%s",s[i]);
            sl[i] = strlen(s[i]);
            point[i] = 0;
            ans = max(ans,sl[i]);
        }
        flag = 0;
        while(1)
        {
            dfs(ans);
            if(flag)break;
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值