HDU1560 DNA sequence
题目:
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.
输入:
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.
输出:
For each test case, print a line containing the length of the shortest sequence that can be made from these sequences.
样例:

分析:公共序列只要满足子序列的排列顺序就行;
IDA*算法,逐渐放大搜索的宽度,并用估价函数(估计仍需匹配的深度)进行剪枝
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cctype>
#define PI acos(-1.0)
const int INF = 0x3f3f3f3f;
const int NINF = -INF - ;
typedef long long ll;
using namespace std;
int n, deep, ans;//deep记录深度, ans记录答案
string seq[];//n个子序列
int siz[];//n个子序列对应的长度
char DNA[] = {'A', 'C', 'G', 'T'};
void dfs(int rec, int *pos)//当前匹配到的公共序列的个数, 个子序列匹配到的位置
{
if (rec > deep) return;//如果大于搜索深度即加深深度结束
int hx = ;//估计剩余需匹配的深度
for (int i = ; i < n; ++i)
{
int temp = siz[i] - pos[i];
hx = max(temp, hx);//剩余需匹配深度为子序列未匹配部分最大长度
}
if (!hx)//如果剩余需匹配为0即完成
{
ans = rec;
return;
}
if (hx + rec > deep) return;//如果已匹配深度加估计剩余需匹配深度大于限制深度即加深深度结束
for (int i = ; i < ; ++i)//公共序列下一个值可能的四个
{
int tmp[];//类似BFS的操作
int flag = ;//flag进行了一次剪枝,如果DNA[i]不能匹配当前任何子序列的下一个值则不再进行DFS直接舍弃(从3500ms优化到1200ms)
for (int j = ; j < n; ++j)
{
if (seq[j][pos[j]] == DNA[i])//子序列j的第pos[j](匹配到位置)若等于
{
flag = ;
tmp[j] = pos[j] + ;//匹配成功下次考虑该子序列的下一个位置
}
else tmp[j] = pos[j];
}
if (flag)
dfs(rec + , tmp);
if (ans != -) return;
}
}
int main()
{
int T;
cin >> T;
while (T--)
{
cin >> n;
int maxn = ;//n个子序列的最大长度
for (int i = ; i < n; ++i)
{
cin >> seq[i];
siz[i] = seq[i].length();//估价函数需要,记录n个子序列对应长度
maxn = max(maxn, siz[i]);
}
deep = maxn;
int pos[];//子序列匹配到的位置
memset(pos, , sizeof(pos));
ans = -;
while ()
{
dfs(, pos);
if (ans != -) break;
deep++;//每进行一次DFS,若未匹配完成则加深搜索深度继续进行
}
cout << ans << endl;
}
return ;
}
HDU1560 DNA sequence的更多相关文章
- HDU1560 DNA sequence(IDA*)题解
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- Hdu1560 DNA sequence(IDA*) 2017-01-20 18:53 50人阅读 评论(0) 收藏
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU1560 DNA sequence —— IDA*算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Oth ...
- HDU1560 DNA sequence IDA* + 强力剪枝 [kuangbin带你飞]专题二
题意:给定一些DNA序列,求一个最短序列能够包含所有序列. 思路:记录第i个序列已经被匹配的长度p[i],以及第i序列的原始长度len[i].则有两个剪枝: 剪枝1:直接取最长待匹配长度.1900ms ...
- 【HDU - 1560】DNA sequence (dfs+回溯)
DNA sequence 直接中文了 题目描述 21世纪是生物科技飞速发展的时代.我们都知道基因是由DNA组成的,而DNA的基本组成单位是A,C,G,T.在现代生物分子计算中,如何找到DNA之间的最长 ...
- POJ 2778 DNA Sequence(AC自动机+矩阵加速)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9899 Accepted: 3717 Desc ...
- POJ 2778 DNA Sequence (AC自己主动机 + dp)
DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...
- hdu 1560 DNA sequence(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others) ...
- poj 2778 DNA Sequence AC自动机
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11860 Accepted: 4527 Des ...
随机推荐
- which
功能说明:显示命令的全路径. 参数选项: -a 默认在PATH路径中由前往后查找命令,如果查找到了,就停止匹配.使用-a选项将遍历所有PATH路径,输出所有匹配项. 参数-a把所有匹配命令路 ...
- CStatic设置位图
CStatic 用于显示位图 如果要显示图标,则必须要设置窗口属性为 SS_BITMAP 和 SS_CENTERIMAGE,实例代码如下: //获得指向静态控件的指针 CStatic *pStatic ...
- 文件上传原理--FileReader
单个文件:<div> <input value="上传" type="file" id="photos_upload"&g ...
- 在iOS项目中嵌入RN代码
1:在项目跟目录下创建一个ReactComponent文件夹.目录结构如下: 2: 在ReactComponent文件夹下新建一个 package.json 文件 { "name" ...
- 解决hibernate删除时的异常 deleted object would be re-saved by cascade (remove deleted object from associa
今天在做项目时,需要删除一个对象,由于关联关系是一对多和多对一的关系,于是在代码中需要删除多的一方的对象时出现了 deleted object would be re-saved by cascade ...
- 【Apache Kafka】一、Kafka简介及其基本原理
对于大数据,我们要考虑的问题有很多,首先海量数据如何收集(如Flume),然后对于收集到的数据如何存储(典型的分布式文件系统HDFS.分布式数据库HBase.NoSQL数据库Redis),其次存储 ...
- vue上传阿里云图片组件
首先需要弄一个阿里云存储.然后配置一下.前端就可以直接上传图片并回显.可在父级组件定义上传图片类型以及大小.默认为500kb.样式可以自适应调整. <template> <div c ...
- nexus3.x启动不起来
1.首先说两种启动命令,网上最多的是用./nexus start.这种是后台启动,看不到实时日志:./nexus run 是实时启动可以看到日志. 2.linux下解压nexus-3.6.2-01-u ...
- Linux常用解压缩命令
压 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称 查 询:tar -jtv -f filename.tar.bz2 解压缩:tar -jxv -f filen ...
- 49.ardinality算法之优化内存开销以及HLL算法
主要知识点 precision_threshold参数的理解 HLL算法优化(hash) cardinality,count(distinct),5%的错误率,性能在100ms左右 ...