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 ...
随机推荐
- (转) Hibernate注解开发
http://blog.csdn.net/yerenyuan_pku/article/details/70162268 Hibernate注解开发 在Hibernate中我们一般都会使用注解,这样可以 ...
- C# 调用指定打印机 (并不是默认)
this.printDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; this.pr ...
- js的replace, 高亮
";console.log(str.replace(/\,/g, "")); //输出 123 ";console.log(str);//输出123 " ...
- Python总结2
时间:25日上午'''列表定义:在[]内,可以存放多个任意类型的值,并以逗号隔开''''students=['sb','2b']print(students[1])student_info=['min ...
- Python3爬取前程无忧数据分析工作并存储到MySQL
1.导入包import requests #取数from lxml import etree #用xpath解析import pymysql #连接数据库import chardet #自动获取编码2 ...
- Linux 开启443端口
1 在Linux终端输入指令: iptables -I INPUT -p tcp --dport 443 -j ACCEPT 2 回车之后继续输入指令,输入保存防火墙配置指令: service ...
- iptables详解(2):iptables实际操作之规则查询
所属分类:IPtables Linux基础 在阅读这篇文章之前,请确保你已经阅读了如下文章,如下文章总结了iptables的相关概念,是阅读这篇文章的基础. 图文并茂理解iptables 如果你是一 ...
- 【原】Python学习_Django搭建环境及创建第一个项目
1.Window 平台安装 Python 下载安装包 https://www.python.org/downloads/windows/ 2.Pyhton环境变量配置 右键点击"计算机 ...
- colgroup 整行变色
<table border="2" width="100%"> <colgroup span="2" align=&quo ...
- Tkinter图形界面设计(GUI)
[因为这是我第一个接触的GUI图形界面python库,现在也不用了,所以大多数内容都来自之前花 钱买的一些快速入门的内容,可以当作简单的知识点查询使用] 在此声明:内容来自微信公众号GitChat,付 ...