HDU-1560-DNA sequence(迭代深搜)

DNA sequence

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

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

题意:找到一个序列,使得所有给出的字符串都是此序列的子串,求出此序列的最短长度

思路:显然最短长度是所有给出字符串的最大长度,接着可以往后不停的枚举答案,深搜确定枚举的答案对不对

代码

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
//迭代深搜好难
char map[10][10];//存图
int num[10];//第i行有num[i]个字符
int point[10];第i行匹配到了point[i]
char str[]="ACGT";
bool flag;//如果找到解了标记为true
int N;//每组数据有N行字符串
int wuyang()//返回还需要在当前搜索路径基础上最少增加多少字符可以匹配成功
{
    int result=0;
    for(int i=0; i<N; i++) //找出N个字符串还需要的最大字符数量
    {
        result=max(result,num[i]-point[i]);
    }
    return result;
}
void DFS(int steap)//传入剩余搜索字符数量,和wuyang的返回值做对比
{
    if(wuyang()==0)//找到目标解
    {
        flag=true;
        return;
    }
    if(wuyang()>steap)
        return;
    int flag_point[10];
    for(int i=0; i<N; i++) //搜索之前先保护现场
        flag_point[i]=point[i];
    bool yes=false;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<N; j++)
            if(map[j][point[j]]==str[i])
            {
                point[j]++;
                yes=true;
            }
        if(yes)//对于str[i]字符,如果找到匹配字符
        {
            DFS(steap-1);//继续下一层深搜
            if(flag==true)//搜索成功直接返回
                return;
            for(int k=0; k<N; k++)//搜索失败恢复现场
                point[k]=flag_point[k];
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(map,'\0',sizeof(map));
        memset(num,0,sizeof(num));
        memset(point,0,sizeof(point));
        scanf("%d",&N);
        int result=0;
        for(int i=0; i<N; i++)
        {
            scanf("%s",map[i]);
            num[i]=strlen(map[i]);
            point[i]=0;
            result=max(result,num[i]);//从所有字符串的最大长度开始枚举
        }
        flag=false;//初始化未找到结果
        while(1)
        {
            DFS(result);
            if(flag==true)
            {
                printf("%d\n",result);
                break;
            }
            result++;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值