题目:

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. 通过offset值的设置使html元素对齐

    今天是我第一次写这个随笔,为了记录我发现的一个jquery的offset的值的问题. 这个offset的值会因为页面标签是否处于隐藏状态而表现出不同的值,隐藏状态时,offset的值是相对于直接父亲的 ...

  2. PHP 之递归遍历目录与删除

    /** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...

  3. Redis 之持久化(rdb、aof)

    Redis的持久化有2种方式   1快照  2是日志 测试aof:

  4. jinkins配置python虚拟环境

    cd /root/.virtualenvs . automationVenv/bin/activate cd /home/wangju/gitProject/automationtest#cd pyt ...

  5. Centos7安装gitlab服务器

    1.先按照官方教程 https://about.gitlab.com/downloads/#centos7 大概内容如下: 1. Install and configure the necessary ...

  6. mybatis 简单的入门实例

    第一步:添加mybaties的架包 第二步:配置mybaties的文件 <?xml version="1.0" encoding="UTF-8" ?> ...

  7. react 子组件调用父组件方法

    import React from 'react'import '../page1/header.css'import { Table } from 'antd'import Child from ' ...

  8. ie 浏览器下ajax请求来自缓存的解决方法

    如上图所示,在ie浏览器下发出的请求,如何缓存中已经出现过这条请求记录,则不会请求服务端数据,解决方法是在请求后增加一个随机数,使每次请求都不同*可以添加当前时间戳 url+'?t='+Date.no ...

  9. 198. House Robber(动态规划)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  10. 【codeforces 510B】Fox And Two Dots

    [题目链接]:http://codeforces.com/contest/510/problem/B [题意] 让你在一个二维的方格里面找环; 两个点有相邻的边它们才是相连的; 有环YES,没环NO ...