DNA sequence

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Author

LL

Source

HDU 2006-12 Programming Contest
 
 

使用dfs进行搜索,但限制递归深度。

逐步加深搜索深度,直至找到答案。

主函数中, 限制搜索深度,如果无解,就加深1层深度

强力剪枝: 递归函数中, 首先计算最坏情况下,还需要补充长度:

为每个DNA序列还未匹配的长度之和(sum)。

如果现在搜索深度+sum>限定的搜索深度,则返回

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char f[]={'A','T','G','C'};
int flag,i,t,n,maxlen;
int cnt[];
char str[][];
void dfs(int len,int cnt[])
{
if (flag || len>maxlen) return; int sum=;
for(int i=;i<n;i++) //关键 :ida*(迭代加深搜索)
{
int l=strlen(str[i]);
sum=max(sum,l-cnt[i]);
}
if (sum+len>maxlen) return;
if (sum==) {flag=; return;} for(int i=;i<;i++)
{
char x=f[i];
int next[];
int tflag=;
for(int j=;j<n;j++)
if (str[j][cnt[j]]==x)
{
next[j]=cnt[j]+;
tflag=;
} else next[j]=cnt[j];
if (tflag) dfs(len+,next); //更新了才说明有效
}
return;
}
int main()
{
scanf("%d",&t);
for(;t>;t--)
{
scanf("%d",&n);
maxlen=;
for(i=;i<n;i++)
{
scanf("%s",str[i]);
int l=strlen(str[i]);
maxlen=max( maxlen,l );
}
flag=;
memset(cnt,,sizeof(cnt));
for(i=;i<;i++)
{
dfs(,cnt);
if (flag) break;
maxlen++;
}
printf("%d\n",maxlen);
}
return ;
}

hdu 1560 DNA sequence(迭代加深搜索)的更多相关文章

  1. HDU 1560 DNA sequence(DNA序列)

    HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K  ...

  2. HDU 1560 DNA sequence (IDA* 迭代加深 搜索)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...

  3. HDU 1560 DNA sequence (迭代加深搜索)

    The twenty-first century is a biology-technology developing century. We know that a gene is made of ...

  4. hdu 1560 DNA sequence(搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others)  ...

  5. HDU 1560 DNA sequence(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 题目大意:给出n个字符串,让你找一个字符串使得这n个字符串都是它的子串,求最小长度. 解题思路: ...

  6. HDU 1560 DNA sequence DFS

    题意:找到一个最短的串,使得所有给出的串是它的子序列,输出最短的串的长度,然后发现这个串最长是40 分析:从所给串的最长长度开始枚举,然后对于每个长度,暴力深搜,枚举当前位是哪一个字母,注意剪枝 注: ...

  7. HDU - 1560 DNA sequence

    给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty ...

  8. HDU 1560 DNA sequence A* 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=1560 仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串 ...

  9. HDU1560(迭代加深搜索)

    DNA sequence Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. JQ和其他框架一起使用方法

    时下,越来越多的javascripe框架不断崛起,同时开源网站系统也之间增多.网站建设过程中当使用一些开源的网站程序时,免不了会在javascript上产生冲突.也许网站的开发者习惯使用jQuery, ...

  2. SQL Server服务开闭

    SQL Server(MSSQLSERVER)是必须要开启的,这个是数据库引擎服务,就像汽车的发动机一样. SQL Server代理(MSSQLSERVER)是代理服务,比如你有一些自动运行的,定时作 ...

  3. jquery-选择checkbox的多种策略

    checkbox属性: var val = $("#checkAll").val();// 获取指定id的复选框的值 var isSelected = $("#check ...

  4. 用Jedis获取redis连接(集群和非集群状态下)

    第一:非集群状态下 非集群状态下用Jedis获取redis连接,得到Jedis对象即可,一共有两种: 1.利用Jedis构造器,仅限用于测试,在实际项目中肯定是用JedisPool. Jedis(St ...

  5. C#中泛型默认关键字(default)详解

    我们在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:(T 是引用类型还是值类型?)对此我们将如何处理? C#代码实例: /// <summary&g ...

  6. prestashop 基本设置

    不跳转 要先改这个UPDATE`ps_shop_url`SET`domain_ssl`= replace(`domain_ssl` ,'us2.try.net','www.gelchaussures. ...

  7. spark java 代码example

    https://github.com/apache/spark/tree/master/examples/src/main/java/org/apache/spark/examples

  8. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. Centos7下安装pip

    Linux 通过 pip 安装使用 Shadowsocks - CentOS 7 (06) Pip是安装Python包的工具,提供了安装.列举已安装包.升级以及卸载包的功能.Pip 是对easy_in ...

  10. servlet第2讲(上集)