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 ;
}

最新文章

  1. AppBox升级进行时 - 扁平化的权限设计
  2. calc()问题
  3. Node.js开发环境搭建
  4. session 登陆浏览,并实现session注销登陆
  5. linux,下载与安装
  6. 【java】 java 集合类UML图
  7. noj [1482] 嘛~付钱吧!(完全背包)
  8. pyzmq简单的在线聊天室
  9. FindChildControl与FindComponent
  10. Netty学习笔记
  11. [转载] Netty
  12. NODE&amp;NPM
  13. FortiGate高校图书馆SSLvpn配置案例
  14. #46 delete(动态规划+树状数组)
  15. POJ 3414 dfs 回溯
  16. 基于 Tornado 实现的 Web 站点反向代理
  17. 母版页 MasterPage
  18. jQuery之css
  19. 通过Scanner从控制台获取数据
  20. C基础 redis缓存访问

热门文章

  1. switch case 与 if
  2. TamperData火狐插件启用
  3. 关于SWT中的GridLayout布局方式
  4. 关于Git远程版本库
  5. HTTP层 —— 控制器
  6. div中的img垂直居中
  7. 第九篇、CSS布局
  8. 12天学好C语言——记录我的C语言学习之路(Day 9)
  9. centos 6.4 安装php-fpm 及常用扩展,(转)
  10. Linux C 程序 基础语法(1)