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 ( 迭代加深搜索基础题 )的更多相关文章

  1. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  2. HDU 1560 DNA sequence(DNA序列)

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

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

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

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

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

  5. hdu 1560 DNA sequence(搜索)

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

  6. HDU 1560 DNA sequence(IDA*)

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

  7. HDU 1560 DNA sequence DFS

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

  8. HDU - 1560 DNA sequence

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

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

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

  10. HDU1560(迭代加深搜索)

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

随机推荐

  1. Pycharm 2022 取消双击 shift 搜索框

    Pycharm取消双击shift搜索框 基于PyCharm 2022.3.2 (Professional Edition),旧版本修改方式自行搜索 双击shift弹出搜索框,输入内容double mo ...

  2. Axure实战应用:Axure设计可视化大屏!

    Axure是一款功能强大的原型设计工具,可以用于设计可视化大屏.设计一个有效的可视化大屏需要考虑多个方面,包括布局规划.信息展示.交互设计等. 以下是一个详细的描述,希望对你有所帮助. 第一部分:可视 ...

  3. nginx 反向代理teleport

    nginx 反向代理teleport 普通配置(以Nginx服务与TP服务在同一台主机上为例) # ...其他内容... server { listen 80; server_name www.you ...

  4. nginx+lua+redis实现灰度发布

    前言: 授人以鱼不如授人以渔.先学会用,在学原理,在学创造,可能一辈子用不到这种能力,但是不能不具备这种能力.这篇文章主要是沉淀使用nginx+lua+redis实现灰度,当我们具备了这种能力,随时可 ...

  5. python学习笔记:python的字符串拼接效率分析

    问题的起因是因为在做LeetCode5714题的时候,对于字符串拼接使用了 ans = ans+s[i] 提交后超时了,改成 ans+=s[i] 就可以通过了,而且用c++好像也有这个问题,在此记录一 ...

  6. LIS(比动态规划更快的解法N*logN)

    以[1,3,8,17,5,14,10]为例,首先我们需要开设一个栈S保存,栈中的元素S[i]代表了以S[i]结尾的长度为i+1的最长上升子序列的最小取值(0<=i). 然后执行下列算法步骤: ( ...

  7. C++ Qt开发:StatusBar底部状态栏组件

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍QStatus ...

  8. Scrapy如何在爬虫类中导入settings配置

    假设我们在settings.py定义了一个IP地址池 ##### 自定义设置 IP_PROXY_POOL = ( "127.0.0.1:6789", "127.0.0.1 ...

  9. linux文件和文件夹的筛选、查找及用户和组管理命令

    1.列出当前系统上所有已经登陆的用户的用户名, 注意:同一个用户登录多次,则只显示一次即可. 查看系统用户的命令为: (1)getent passwd 查看系统上的所有用户信息 (2)w 查看登录用户 ...

  10. windows server 2019 IIS网站属性上没有asp.net标签 ,aspnet_regiis -i 不能安装用命令解决

    用如下命令安装: dism /online /enable-feature /featurename:IIS-ASPNET45 /all