DNA sequence

Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 15   Accepted Submission(s) : 7

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

Author

LL

Source

HDU 2006-12 Programming Contest
 
 

使用dfs进行搜索,但限制递归深度。

逐步加深搜索深度,直至找到答案。

主函数中, 限制搜索深度,如果无解,就加深1层深度

强力剪枝: 递归函数中, 首先计算最坏情况下,还需要补充长度:

为每个DNA序列还未匹配的长度之和(sum)。

如果现在搜索深度+sum>限定的搜索深度,则返回

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char f[]={'A','T','G','C'};
int flag,i,t,n,maxlen;
int cnt[];
char str[][];
void dfs(int len,int cnt[])
{
if (flag || len>maxlen) return; int sum=;
for(int i=;i<n;i++) //关键 :ida*(迭代加深搜索)
{
int l=strlen(str[i]);
sum=max(sum,l-cnt[i]);
}
if (sum+len>maxlen) return;
if (sum==) {flag=; return;} for(int i=;i<;i++)
{
char x=f[i];
int next[];
int tflag=;
for(int j=;j<n;j++)
if (str[j][cnt[j]]==x)
{
next[j]=cnt[j]+;
tflag=;
} else next[j]=cnt[j];
if (tflag) dfs(len+,next); //更新了才说明有效
}
return;
}
int main()
{
scanf("%d",&t);
for(;t>;t--)
{
scanf("%d",&n);
maxlen=;
for(i=;i<n;i++)
{
scanf("%s",str[i]);
int l=strlen(str[i]);
maxlen=max( maxlen,l );
}
flag=;
memset(cnt,,sizeof(cnt));
for(i=;i<;i++)
{
dfs(,cnt);
if (flag) break;
maxlen++;
}
printf("%d\n",maxlen);
}
return ;
}

hdu 1560 DNA sequence(迭代加深搜索)的更多相关文章

  1. HDU 1560 DNA sequence(DNA序列)

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

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

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

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

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

  4. hdu 1560 DNA sequence(搜索)

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

  5. HDU 1560 DNA sequence(IDA*)

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

  6. HDU 1560 DNA sequence DFS

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

  7. HDU - 1560 DNA sequence

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

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

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

  9. HDU1560(迭代加深搜索)

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

随机推荐

  1. [ An Ac a Day ^_^ ] CodeForces 691F Couple Cover 花式暴力

    Couple Cover Time Limit: 3000MS   Memory Limit: 524288KB   64bit IO Format: %I64d & %I64u Descri ...

  2. TimeJob权限问题 拒绝访问

    internal void RenameWithoutValidation(string value) {     if (value == null) throw new ArgumentNullE ...

  3. 杭电21题 Palindrome

    Problem Description A palindrome is a symmetrical string, that is, a string read identically from le ...

  4. 使用observable数组(Working with observable arrays)

    observable数组(observable arrays) 如果你要探测和响应一个对象的变化,你应该用observables.如果你需要探测和响应一个集合对象的变化,你应该用observableA ...

  5. yii2.0使用ActionForm创建表单

    文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...

  6. spring框架--IOC容器,依赖注入

    思考: 1. 对象创建创建能否写死? 2. 对象创建细节 对象数量 action  多个   [维护成员变量] service 一个   [不需要维护公共变量] dao     一个   [不需要维护 ...

  7. bat自动创建文件夹(以当前时间命名)

    先cmd中查看当前的日期和时间: @echo off color 0a set dt=%date%%time% echo %dt%pause 1.使用截取进行命名(时间为12小时制时命名会出现空格,不 ...

  8. hdu_1115_Lifting the Stone(求多边形重心)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1115 题意:给你N个点围成的一个多边形,让你求这个多边形的重心. 题解: 将多边形划分为若干个三角形. ...

  9. nginx 部署多网站

    1, www 下面加一个文件夹 abc 2,   在default.conf 复制一下 ,abc.conf , 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  10. app调用支付宝支付 笔记

    1.提交各种申请 2.通过后进入支付宝开放平台  --> 管理中心 -->创建应用  --> 填写相关信息 提交等待审核通过(1,2天)   3.下载集成包(https://doc. ...