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 ...
随机推荐
- 常用sql语句(不定时更新)
--查询数据库所有表名与表说明 select a.name tableName, b.value tableComment from sysobjects a LEFT JOIN sys.extend ...
- [ABC263F] Tournament
Problem Statement $2^N$ people, numbered $1$ to $2^N$, will participate in a rock-paper-scissors tou ...
- Rong晔大佬教程学习(1):背景与项目设计目标
riscv实际上是一种ISA的指令集,而处理器的设计的基本结构是不变的(如下所示),其区别在于所选用的指令集的类型,一般有ARM.RISCV.MIPS等,采用了不同的引擎,那么车的外观.系统等也会随之 ...
- style绑定及随机颜色
一个小案例 颜色变换 style单机事件的绑定
- 如何判断lib和dll是32位还是64位?答案是使用微软的dumpbin工具,后面讲了如何使用gcc生成lib和dll
为什么我会考虑这个问题呢?因为我在使用java去调用一个c的lib库的时候,弹出以下警告: D:\work\ideaworkpaces\jdk21Test001\src\main\java\lib\h ...
- 3D 高斯点染简介
3D 高斯点染技术由 3D Gaussian Splatting for Real-Time Radiance Field Rendering 一文首次提出.作为一种栅格化技术,3D 高斯点染可用于实 ...
- 屎山代码风格指南(避免被优化&&避免被接盘)
欢迎补充!!! 序言 良好的代码结构:Bad 使用有意义的变量和函数名,遵循命名规范,使代码易于理解. 组织代码,使用适当的文件和文件夹结构,保持模块化. 避免全局变量的滥用,尽量使用局部作用域. 单 ...
- Win10笔记本开启热点让手机上网
Win10开启热点让手机上网,笔记本电脑开启热点,笔记本电脑开启WiFi,手机连接笔记本电脑热点上网. 1.前提条件:笔记本电脑网线联网,笔记本电脑支持热点 2.胆子要大,不怕公司网管统计发现你的电脑 ...
- 文心一言 VS 讯飞星火 VS chatgpt (175)-- 算法导论13.3 4题
四.用go语言,Teach 教授担心 RB-INSERT-FIXUP可能将 T.nil.color 设为 RED,这时,当 z 为根时第1行的测试就不会让循环终止.通过讨论 RB-INSERT-FIX ...
- 文心一言 VS 讯飞星火 VS chatgpt (176)-- 算法导论13.3 5题
五.用go语言,考虑一棵用 RB-INSERT 插人 n 个结点而成的红黑树.证明:如果 n>1,则该树至少有一个红结点. 文心一言: 要证明这个问题,我们首先需要理解红黑树的性质.红黑树是一种 ...