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以上,碉堡了:
有图有真相:

代码:
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<queue>
  5.  
  6. #define M 1679616+100000
  7. using namespace std;
  8.  
  9. struct Nod
  10. {
  11. int st;
  12. int ln;
  13. }nd1,nd2;
  14.  
  15. int hash[M];
  16. int n,len[],size,maks;
  17. char str[][];
  18. char ku[] ="ACGT";
  19. int cas =; // hash标记用,这里可以节省大量初始化时间(大神专用,哈哈,看的大神代码都这么玩的)
  20.  
  21. int bfs()
  22. {
  23. int pos[];
  24. queue<Nod> q;
  25. nd1.st = ;
  26. nd1.ln = ;
  27. q.push(nd1);
  28. // memset(hash,0,sizeof(hash)); //用上面的cas代替初始化过程,节省时间1s以上
  29. while(!q.empty())
  30. {
  31. nd2 = q.front();
  32. q.pop();
  33. int i,j;
  34. int st = nd2.st;
  35. for(i=;i<n;i++)
  36. {
  37. pos[i] = st%maks;
  38. st/=maks;
  39. }
  40. for(j=;j<;j++)
  41. {
  42. st = ;
  43. for(i=n-;i>=;i--)
  44. {
  45. st = st * maks + pos[i] + (str[i][pos[i]] == ku[j]);
  46. }
  47. if(hash[st] == cas) continue;
  48. if(st == size) return nd2.ln + ;
  49. hash[st] = cas;
  50. nd1.ln = nd2.ln + ;
  51. nd1.st = st;
  52. q.push(nd1);
  53. }
  54. }
  55. return -;
  56. }
  57.  
  58. int main()
  59. {
  60. int t;
  61. scanf("%d",&t);
  62. cas = ;
  63. while(t--)
  64. {
  65. scanf("%d",&n);
  66. int i;
  67. maks=;
  68. for(i=;i<n;i++)
  69. {
  70. scanf("%s",str[i]);
  71. len[i] = strlen(str[i]);
  72. if(maks<len[i]) maks = len[i];
  73. }
  74. maks++;
  75. size = ;
  76. for(i=n-;i>=;i--) size = size*maks+len[i];
  77. printf("%d\n",bfs());
  78. cas++; //cas加1,用来标记下一组测试数据
  79. }
  80. return ;
  81. }

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. -bash: lampp: command not found解决方案

    在/opt目录下安装完lampp后,需要到/opt/lampp/下执行lampp启动或者停止服务,如果在其余目录下执行lampp,会提示:-bash: lampp: command not found ...

  2. my_vimrc

    " ----------------- Author: Ruchee" ----------------- Email: my@ruchee.com" --------- ...

  3. 亲测 asp.net 调用 webservice返回json

    前端脚本 $("#sure").click(function () { var tbody = $("#putsigal tbody"); var trs = ...

  4. dede版权信息修改

    login:dede-templets-login.htm 系统主页:dede-templets-index2.htm 主体内容在index_body.htm文件   干掉: $(function() ...

  5. JDK Tools - xjc: 将 XML Schema 编译成 Java 类

    xjc 是 JAXB 将 xsd 生成 Java 类的工具. 命令格式 xjc [ options ] schema file/URL/dir/jar ... [-b bindinfo ] ... 命 ...

  6. casperjs 抓取爱奇艺高清视频

    CasperJS 是一个开源的导航脚本和测试工具,使用 JavaScript 基于 PhantomJS 编写,用于测试 Web 应用功能,Phantom JS是一个服务器端的 JavaScript A ...

  7. vc静态加载dll和动态加载dll

    如果你有a.dll和a.lib,两个文件都有的话可以用静态加载的方式: message函数的声明你应该知道吧,把它的声明和下面的语句写到一个头文件中 #pragma comment(lib, &quo ...

  8. .net远程连接oracle数据库不用安装oracle客户端的方法

    .net远程连接oracle数据库不用安装oracle客户端的方法步骤: 1.添加Sytem.Data.OracleClient命名空间. 2.连接时需要ConnectionString字符串,出现在 ...

  9. c++ 继承学习笔记

    三大继承原则(由我杜撰) 基类的私有成员被继承后不可见(优先级最高) 公有继承不改变基类成员属性 保护继承(私有继承)把基类成员变为保护成员(私有成员)

  10. C++ 代码性能优化 -- 循环分割提高并行性

    对于一个可结合和可交换的合并操作来说,比如整数的加法或乘法, 我们可以通过将一组合并操作分割成 2 个或更多的部分,并在最后合并结果来提高性能. 原理: 普通代码只能利用 CPU 的一个寄存器,分割后 ...