HDU - 1560:DNA sequence ( 迭代加深搜索基础题 )
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
题意:
从n个串中找出一个最短的公共串,,该公共串对于n个字符串不要求连续,即只要保持相对顺序就好。
思路:
用迭代加深搜索,所谓迭代加深搜索,就是限制DFS的深度,若搜不到答案,则加深深度,重新搜索,这样就防止了随着深度不断加深而进行的盲目搜索,而且,对于这种求最短长度之类的题目,只要找到可行解,即是最优解了。同时注意剪枝,每次DFS的时候,都要判断一下,当前的深度+最少还有加深的深度是否大于限制的长度,若是,则退回上一层状态。
#include<bits/stdc++.h>
using namespace std;
char str[10][10];//记录n个字符串
int n, ans, deep, Size[10];
char DNA[4] = { 'A','C','G','T' };//4种可能性
void dfs(int cnt,int len[]) {
if (cnt > deep)return;//大于了最大深度
int maxx = 0;//预计还要匹配的字符串的最大长度
for (int i = 0; i < n; ++i) {
int t = Size[i] - len[i];
if (t > maxx)
maxx = t;
}
if (maxx == 0) {//条件全部满足即为最优解
ans = cnt; return;
}
if (cnt + maxx > deep)return;
for (int i = 0; i < 4; ++i) {
int pos[10], flag = 0;
for (int j = 0; j < n; j++)
{
if (str[j][len[j]] == DNA[i])
{
flag = 1;
pos[j] = len[j] + 1;
}
else
pos[j] = len[j];
}
if (flag)
dfs(cnt + 1, pos);
if (ans != -1)
break;
}
}
int main() {
int T;
cin >> T;
while (T--) {
cin >> n;
int maxn = 0;
for (int i = 0; i < n; ++i) {
cin >> str[i];
Size[i] = strlen(str[i]);
if (Size[i] > maxn)
maxn = Size[i];
}
ans = -1; deep = maxn;
int pos[10] = { 0 };//记录n个字符串目前匹配到的位置
while (1) {
dfs(0, pos);
if (ans != -1)break;
++deep;
}
cout << ans << endl;
}
return 0;
}
HDU - 1560:DNA sequence ( 迭代加深搜索基础题 )的更多相关文章
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 1560 DNA sequence(DNA序列)
HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- HDU 1560 DNA sequence (迭代加深搜索)
The twenty-first century is a biology-technology developing century. We know that a gene is made of ...
- hdu 1560 DNA sequence(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others) ...
- HDU 1560 DNA sequence(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 题目大意:给出n个字符串,让你找一个字符串使得这n个字符串都是它的子串,求最小长度. 解题思路: ...
- HDU 1560 DNA sequence DFS
题意:找到一个最短的串,使得所有给出的串是它的子序列,输出最短的串的长度,然后发现这个串最长是40 分析:从所给串的最长长度开始枚举,然后对于每个长度,暴力深搜,枚举当前位是哪一个字母,注意剪枝 注: ...
- HDU - 1560 DNA sequence
给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty ...
- HDU 1560 DNA sequence A* 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=1560 仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串 ...
- HDU1560(迭代加深搜索)
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- CART算法解密:从原理到Python实现
本文深入探讨了CART(分类与回归树)算法的核心原理.实现方法以及应用场景.文章首先介绍了决策树的基础知识,然后详细解析了CART算法的工作机制,包括特征选择和树的构建.接着,通过Python和PyT ...
- K8s容器debug高级技巧
使用 kubectl exec 执行指令 如果您在 Kubernetes 上运行软件,您会想要在某些时候去调试您所部署的软件的一些方面.对于习惯于使用虚拟机 (VMs) 的人来说能自然使用的一种简单的 ...
- JS对后端响应的long类型数据处理精度丢失问题
1.数据库的数据 2.前端拿到的数据 前端帮我们进行四舍五入了,这并不是我想要的 3.解决办法 把后端响应的数据long类型转成string类型,可以使用Stream流的方式或者for循环的方式,对响 ...
- 针对LocalDateTime日期格式化解决方案
前两天做项目,后端返回给前端的数据中日期时间格式如下: 并不是我想要的yyyy-MM-dd HH:mm:ss格式的,所以网上搜了一下有一个JSON日期LocalDateTime序列化器(如果是Date ...
- 启动android项目时报dx.jar错误的解决方案
启动android项目时遇到如下错误: Failed to load E:\SDK\android-sdk-windows\build-tools\27.0.2\lib\dx.jar 原因: 自动使用 ...
- DEDECMS 后台系统用户授权目录更改为无限级(默认为二级授权)
在做一个学校的项目,分类有四级分类,总共一百多个分类,因为每个分类对应不同的老师,用于上传资料作为考核,但是添加系统用户的时候发现DEDECMS只有两级分类,所以修改啦一些代码,目前不知道是否修改完全 ...
- 川普真会说中文?连嘴型都同步,用VideoReTalking一键生成你的AI播报员
你能想到这种画面吗?霉霉在节目中用普通话接受采访,特朗普在老家用中文脱口秀,蔡明老师操着一口流利的英文调侃潘长江老师.. 这听起来似乎很魔幻,可如今全部由VideoReTalking实现了 你只需要传 ...
- k8s初始化pod-pod标签
目录 initContainers(初始化容器) 静态pod pod的调度策略(将pod指派给特定节点) initContainers(初始化容器) k8s在1.3版本的时候引入了一个初始化容器(in ...
- 提取 PE文件 / 目标程序 的各种信息
前段时间项目需要实现对 Windows PE 文件版本信息的提取,如文件说明.文件版本.产品名称.版权.原始文件名等信息.获取这些信息在 Windows 下当然有一系列的 API 函数供调用,简单方便 ...
- 《架构整洁之道》学习笔记 Part 1 概述
本书主题 介绍什么是优秀的软件架构,以提高软件架构质量 介绍系统架构的各种属性与成本和生产力的关系,以采用好的设计和架构以便减少构建成本 好的软件架构可以带来什么? 大大节省软件项目构建与维护的人力成 ...