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. 看病要排队--hdu1873

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1873 运用优先队列写就行了 #include<stdio.h> #include< ...

  2. Web Responsive Table, 只需CSS使table在手机和平板中完美显示

    在做responsive或者手机版页面的时候,经常碰到<Table>在手机和平板中会因为长度问题把页面撑大.最近看到一个比较好,比较方便的方法,而且仅仅用CSS 2就可以实现! 实例URL ...

  3. Spark Streaming性能优化: 如何在生产环境下应对流数据峰值巨变

    1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...

  4. [py][mx]django课程模型

    课程模型分析 分3个表 先设计课程表, 这是1 在设计lesson表,添加一个外键,course. 课程 1 course 章节 n lesson 视频 n video 资源 n coursereso ...

  5. [LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst

    Remove all elements from a linked list of integers that have value val. Example: Input: 1->2-> ...

  6. EXTJS 4.2.1.883 Summary 合计栏宽度bug

    EXTJS 4.2.1.883中改进了summary插件,使合计栏能够在grid最底部显示,但是列宽和表格对不上,解决方法: 找到以下样式 .x-docked-summary .x-grid-tabl ...

  7. python 的 json 转换

    python 的 json 转换 本文为原创文章,禁止转载! 本文以 json.dumps()  和 json.loads() 方法进行 Python 数据和 json 格式之间转换,进行讲解 首先比 ...

  8. Jmeter接口自动化测试 (四)(持续构建)

    本文转载至http://www.cnblogs.com/chengtch/p/6145867.html  Jmeter是压力测试.接口测试工具,Ant是基于Java的构建工具,具有跨平台的作用,jen ...

  9. C++静态数据成员与静态成员函数

    一般情况下,如果有n个同类的对象,那么每一个对象都分别有自己的数据成员,不同对象的数据成员各自有值,互不相干.但是有时人们希望有某一个或几个数据成员为所有对象所共有,这样可以实现数据共享. 可以使用全 ...

  10. BootStrap的布局学习

    布局组件无数可复用的组件,包括字体图标.下拉菜单.导航.警告框.弹出框等更多功能. Bootstrap的使用非常灵活,可以对各种组件进行合并使用(如:为标签页项 添加带下拉菜单),下面的知识点中将逐个 ...