DNA sequence

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3503    Accepted Submission(s): 1681

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

14ACGTATGCCGTTCAGT
 
Sample Output

8

思路:

刚开始BFS就爆内存了。

新思路是给dfs加一个最小限制,超过限制就返回,然后不断加大限制直到符合,那么此时dfs的答案也是最小的。这里有几个要剪枝的地方:一是超过限制剪枝;二是预估值+当前值超过限制也要剪枝。

一开始看的那些题解都看不懂的我emmmm......orz

借鉴题解:链接

Code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
//#include<map>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
const int N=810;
using namespace std;
char str[10][10],dna[4]={'A','C','G','T'};
int n,deep,ans,len[10];
void dfs(int step,int pos[]){ //step为当前长度
if(step>deep) return; //超过深度返回
int maxdeep=0;
for(int i=0;i<n;i++){
maxdeep=max(len[i]-pos[i],maxdeep); //maxdeep为预估剩余深度
}
if(step+maxdeep>deep) return; //当前长度加预估剩余深度大于deep,剪枝
if(maxdeep==0){ //所有串都满足
ans=deep;
return;
}
int temp[10],flag;
for(int i=0;i<4;i++){
flag=0;
for(int j=0;j<n;j++){
if(str[j][pos[j]]==dna[i]){
temp[j]=pos[j]+1;
flag=1;
}
else temp[j]=pos[j];
}
if(flag){
dfs(step+1,temp);
}
if(ans) return;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
deep=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",str[i]);
len[i]=strlen(str[i]);
deep=max(deep,len[i]); //找出最长的作为第一次深搜最小深度
}
ans=0;
int pos[10]; //表示第i组验证到第pos[i]个
memset(pos,0,sizeof(pos));
while(1){
dfs(0,pos);
if(ans) break;
deep++; //加深迭代深度,重新DFS
}
printf("%d\n",ans);
}
return 0;
}

HDU1560 DNA sequence(IDA*)题解的更多相关文章

  1. HDU1560 DNA sequence —— IDA*算法

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

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

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

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

  4. HDU1560 DNA sequence

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

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

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

  6. HDU 1560 DNA sequence(IDA*)

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

  7. HDU 1560 DNA sequence(DNA序列)

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

  8. hdu 1560 DNA sequence(迭代加深搜索)

    DNA sequence Time Limit : 15000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  9. POJ2278 DNA Sequence —— AC自动机 + 矩阵优化

    题目链接:https://vjudge.net/problem/POJ-2778 DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Tota ...

随机推荐

  1. Python开发【笔记】:关于子线程(子进程)与主线程(主进程)的关联

    前言: 主要分析下面的问题: 主线程启线程  主线程执行完毕,会关闭子线程吗? 子线程启线程  主线程执行完毕,会结束吗? 主进程启动进程,主进程执行完毕,会怎样? 子进程启动进程,进程执行完毕,又会 ...

  2. 地理位置geo处理之mysql函数

    目前越来越多的业务都会基于LBS,附近的人,外卖位置,附近商家等等,现就讨论离我最近这一业务场景的解决方案. 原文:https://www.jianshu.com/p/455d0468f6d4 目前已 ...

  3. 共享访问在.NET中的编程实现

    转载:http://blog.csdn.net/zhzuo/article/details/1732937 共享访问在.NET中的编程实现 发布日期:2007-08-08 | 更新日期:2009-03 ...

  4. fiddler抓包工具使用

    此工具用于抓取302等看不到的包. 设置: 步骤一 步骤二 重启fiddler软件,设置才有效. 设置谷歌浏览器,使浏览器的访问都经过fiddler.(fiddler就成了代理了)设置如下 隐藏图片的 ...

  5. wordpress注册后重定向到自定义页面

    wordpress注册后重定向到自定义页面怎么操作?将下面的代码添加到当前主题的 functions.php 文件中即可 add_filter( 'registration_redirect', 'w ...

  6. Python3学习之路~2.1 列表、元组操作

    列表 列表是我们以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作. 定义列表(list) names=['Amy','Bob','Cindy','David'] 通过下标访问列 ...

  7. linux下面发布80端口的服务

    1:linux下面发布端口号为80 的服务,要在root用户下面发布.否则提示权限不够 2:在普通用户下面配置的java环境,在root用户下面不可用. 解决方法:2.1  要使用source /et ...

  8. Spark Shuffle Write阶段磁盘文件分析

    这篇文章会详细介绍,Sort Based Shuffle Write 阶段是如何进行落磁盘的 流程分析 入口处: org.apache.spark.scheduler.ShuffleMapTask.r ...

  9. dp训练

    根据这位大佬的https://www.cnblogs.com/Bunnycxk/p/7360183.html 题目链接:https://www.luogu.org/problemnew/show/P3 ...

  10. [LeetCode] 851. Loud and Rich_ Medium tag: DFS

    In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and ...