题目:

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的更多相关文章

  1. HDU1560 DNA sequence(IDA*)题解

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

  2. 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 ...

  3. HDU1560 DNA sequence —— IDA*算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Oth ...

  4. HDU1560 DNA sequence IDA* + 强力剪枝 [kuangbin带你飞]专题二

    题意:给定一些DNA序列,求一个最短序列能够包含所有序列. 思路:记录第i个序列已经被匹配的长度p[i],以及第i序列的原始长度len[i].则有两个剪枝: 剪枝1:直接取最长待匹配长度.1900ms ...

  5. 【HDU - 1560】DNA sequence (dfs+回溯)

    DNA sequence 直接中文了 题目描述 21世纪是生物科技飞速发展的时代.我们都知道基因是由DNA组成的,而DNA的基本组成单位是A,C,G,T.在现代生物分子计算中,如何找到DNA之间的最长 ...

  6. POJ 2778 DNA Sequence(AC自动机+矩阵加速)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9899   Accepted: 3717 Desc ...

  7. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  8. hdu 1560 DNA sequence(搜索)

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

  9. poj 2778 DNA Sequence AC自动机

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

随机推荐

  1. (转)Quartz任务调度(1)概念例析快速入门

    http://blog.csdn.net/qwe6112071/article/details/50991563 Quartz框架需求引入 在现实开发中,我们常常会遇到需要系统在特定时刻完成特定任务的 ...

  2. jmeter解决中文乱码问题

    问题: 当响应数据或响应页面没有设置编码时,jmeter会按照jmeter.properties文件中,sampleresult.default.encoding 设置的格式解析默认ISO-8859- ...

  3. input按钮的事件处理大全

    input按钮的事件处理大全   input按钮的事件处理大全1.<INPUT onclick=document.all.WebBrowser.ExecWB(1,1) type=button v ...

  4. Java中内部类详解—匿名内部类

    什么是内部类? 将一个类A定义在另一个类B里面,里面的那个类A就称为内部类,B则称为外部类.   成员内部类 定义在类中方法外的类. 定义格式: class 外部类 { class 内部类{ } } ...

  5. (Entity framework 应用篇)把权限判断封装在数据库访问层

    这里,我只是以一个例子,说一下简单权限控制,通过这个例子,大家可以设计庞大的权限管理层,把权限控制封装到数据库访问层,这样程序员就不用再写权限判断的代码了 首先,先看看我数据库DBContext的定义 ...

  6. Python 内置函数 day4

    import random s = 'abczfg' st= {3,4,9,1,8} print(dir(random))#打印模块内的方法,输出模块/变量可以调用的方法 print(dir(s))# ...

  7. sysbench测试阿里云CPU

    参考 https://wiki.mikejung.biz/Benchmarking 买了一个1核的ECS,测试一下CPU性能 第一次是只用1个thread去跑 [root@iZwz9fy718twfi ...

  8. sysbench基准测试工具

    一.简介SysBench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.当前功能允许测试的系统参数有:file I/O performance (文件I ...

  9. enote笔记语言(5)——其他

    章节:其他   ((主:单词))                               用来醒目地强调这个句子中哪个词语作主语 sentence:                         ...

  10. Huawei-R&S-网络工程师实验笔记20190530-FTP上传下载、STelnet登录、SFTP登录

    >Huawei-R&S-网络工程师实验笔记20190530-FTP上传下载.STelnet登录.SFTP登录 >>实验开始,参考<Huawei-R&S-网络工程 ...