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 ...
随机推荐
- [编程]UML语言:理论之光与实践之惑
UML介绍及现状 UML(统一建模语言)是软件工程领域中具有悠久历史的一种模型化语言工具.它通过标准化的图形符号体系,使得软件系统的蓝图能够被更直观地表达出来.UML诞生于20世纪90年代,经过多年积 ...
- 使用Tensorrt部署,C++ API yolov7_pose模型
使用Tensorrt部署,C++ API yolov7_pose模型 虽然标题叫部署yolov7_pose模型,但是接下来的教程可以使用Tensorrt部署任何pytorch模型. 仓库地址:http ...
- 利用 Excel 对学生的成绩进行分析管理
利用 Excel 对学生的成绩进行分析和管理是一种常见且有效的方法.以下是一些步骤和技巧,以帮助您实施这一过程: 1. 数据输入:将学生成绩数据输入到 Excel 中的一个工作表中.每个学生可以有一行 ...
- 0x05.HelloJAVA
基础知识 java的类目和文件名必须相同(区分大小写) java文件,先编译成字节码(.class文件),然后在JAVA的虚拟机JVM上以解释方式执行字节码 java的项目里面包含了源代码.依赖.配置 ...
- [ABC274D] Robot Arms 2
Problem Statement You are given a sequence $A = (A_1, A_2, \dots, A_N)$ of length $N$ consisting of ...
- Winform RichTextBox 控件文本内容自动滚动到最后一行
RichTextBox 控件文本内容始终显示追加的最新内容,也就是自动滚动到控件文本框的最后一行. 有两种方法: HideSelection设置为false: // RichTextBox1.Text ...
- 容器网络Cilium:DualStack双栈特性分析
本文分享自华为云社区<容器网络Cilium入门系列之DualStack双栈特性分析>,作者: 可以交个朋友. 一 . 关于IPV6/IPV4 双栈 目前很多公司开始将自己的业务由ipv4切 ...
- LR(0)分析法
LR(0)是一种自底向上的语法分析方法.两个基本动作是移进和规约. 具体例子如下 已知文法G[E] (1) E→aА (2) E→bB (3) A→cА (4) A→d (5) B→cB (6) B→ ...
- Tomcat自动化脚本
/bin/bash war包名称 war_name="tchg.war" 要上传war包指定目录 war_dir="/usr/local/src/tchg" 工 ...
- 文心一言大模型-function Calling的应用
"大模型的函数调用"(Large Model Function Calling)是一个涉及到在大型人工智能模型,如 GPT-4 或类似的高级深度学习模型中使用函数调用的概念.在这种 ...