http://acm.hdu.edu.cn/showproblem.php?pid=1560

DNA sequence

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 732    Accepted Submission(s): 356

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
 
参照大神代码优化hash标记节省时间1S以上,碉堡了:
有图有真相:

代码:
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue> #define M 1679616+100000
using namespace std; struct Nod
{
int st;
int ln;
}nd1,nd2; int hash[M];
int n,len[],size,maks;
char str[][];
char ku[] ="ACGT";
int cas =; // hash标记用,这里可以节省大量初始化时间(大神专用,哈哈,看的大神代码都这么玩的) int bfs()
{
int pos[];
queue<Nod> q;
nd1.st = ;
nd1.ln = ;
q.push(nd1);
// memset(hash,0,sizeof(hash)); //用上面的cas代替初始化过程,节省时间1s以上
while(!q.empty())
{
nd2 = q.front();
q.pop();
int i,j;
int st = nd2.st;
for(i=;i<n;i++)
{
pos[i] = st%maks;
st/=maks;
}
for(j=;j<;j++)
{
st = ;
for(i=n-;i>=;i--)
{
st = st * maks + pos[i] + (str[i][pos[i]] == ku[j]);
}
if(hash[st] == cas) continue;
if(st == size) return nd2.ln + ;
hash[st] = cas;
nd1.ln = nd2.ln + ;
nd1.st = st;
q.push(nd1);
}
}
return -;
} int main()
{
int t;
scanf("%d",&t);
cas = ;
while(t--)
{
scanf("%d",&n);
int i;
maks=;
for(i=;i<n;i++)
{
scanf("%s",str[i]);
len[i] = strlen(str[i]);
if(maks<len[i]) maks = len[i];
}
maks++;
size = ;
for(i=n-;i>=;i--) size = size*maks+len[i];
printf("%d\n",bfs());
cas++; //cas加1,用来标记下一组测试数据
}
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(迭代加深搜索)

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

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

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

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

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

  5. HDU 1560 DNA sequence(IDA*)

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

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

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

  7. HDU 1560 DNA sequence DFS

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

  8. HDU - 1560 DNA sequence

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

  9. POJ1699 HDU 1560 Best Sequence(AC自动机 最短路)

    曾写过迭代加深搜索的方法,现在使用在AC自动上跑最短路的方法 dp[i][j]表示状态为到节点i,模式串是否包含的状态为j的最短串的长度,则状态转移方程为: dp[nx][ny] = min(dp[x ...

随机推荐

  1. [Doc ID 1590988.1]如何清理E-Business Suite的缓存(Apache/iAS, Cabo, Modplsql, Browser, Jinitiator, Java, Portal, WebADI)?

    文档内容   目标   解决方案   参考 适用于: Oracle Applications Technology Stack - 版本 11.5.9 到 12.2.2 [发行版 11.5 到 12. ...

  2. [转]Oracle学习笔记——权限管理

    本文转自:http://www.cnblogs.com/whgw/archive/2011/10/30.html 一.系统的默认用户 1)sys用户是超级用户,具有最高权限,具有sysdba角色,有c ...

  3. Vim 的补全模式加速器,轻松玩转全部 15 种自动补全模式

    1. 关于 Vim 补全模式    ---- Vim 一共提供了 15 种自动补全的模式(:help ins-completion).其中有两种的补全列表内容与另外两种相同,只是排序不同,这 15 种 ...

  4. monkeyrunner工具同Monkey工具的差别

    Monkey: Monkey工具直接运行在设备或模拟器的adb shell中,生成用户或系统的伪随机事件流. monkeyrunner: monkeyrunner工具则是在工作站上通过API定义的特定 ...

  5. JAXB - Unmarshalling

    A simple approach for unmarshalling an XML document consists of the creation of a JAXB context and t ...

  6. Web开发知识点总结

    前言:这是一篇简单的web开发知识点的总结,适用于刚开始学习编程的人来学习的.我是为了能够在熟记熟记这些知识点而总结的一篇文章. 1       什么是浏览器? (1) 浏览器就是接收浏览者的操作(打 ...

  7. [04] SQL语句优化之索引

    1.索引的概念 根据书的目录可以知道内容所在的页码,不用一页一页翻书,可直接通过页码找到内容.数据库的索引类似于书本的目录,索引指向内容存储位置,可直接定位到内容而不必扫描整张表,减少了磁盘的I/O次 ...

  8. C++例题练习(2)

    环境:Dev-C++( Version:5.6.1) 1.循环输入一个1-1000的整数,判断是否为素数(输入1时程序结束) 素数:只能被1和自身整除. 实现代码: #include <iost ...

  9. OC5_NSMutableString操作

    // // main.m // OC5_NSMutableString操作 // // Created by zhangxueming on 15/6/10. // Copyright (c) 201 ...

  10. android Locat工作日志的使用

    #3.Logcat日志工具的使用 日志的等级: error:最高等级,错误信息,红色 warn:比较高,警告信息,橙色 debug:较高,调试信息,蓝色 info:一般,一般信息,绿色 verbose ...