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

    * fang@author * Android布局原则: * (1)尽量多使用LinearLayout和RelativeLAyout,不要使用AbsoluteLayout * (2)在布局层次一样的情 ...

  2. rust haskell

    http://www.rust-lang.org <null>

  3. 转:Internal Sales Order (ISO) Process Flow

    本文介绍下内部销售订单Internal Sales Order(ISO)在Oracle EBS中的流程,内部销售订单和组织间转移(Inter-Organization Transfer,IOT)的作用 ...

  4. 浅试WebStorm配置Node.js开发环境

    web前端开发IDE一直喜欢用WebStorm,这里简单介绍如何用WebStorm搭建一个Node.js开发环境. 首先,需要在本地安装好node.js,以及npm包管理工具.你也可以吧node.js ...

  5. Microsoft Visual Studio 2013 Update 1 离线安装程序

    ☆ 微软官网地址:☆http://www.microsoft.com/zh-cn/download/details.aspx?id=41650☆ 离线安装程序 直接下载链接:☆http://downl ...

  6. freemarker.core.InvalidReferenceException: Expression stackObject.value.get(propertyName).toString() is undefined

    很奇怪的报错, 在实体中添加 toString方法后, 想在前台列表中使用 <s:debug标签调试, 发现报错如下: freemarker.core.InvalidReferenceExcep ...

  7. Jsp与servlet之间页面跳转及参数传递实例(转)

    原网址:http://blog.csdn.net/ssy_shandong/article/details/9328985 11. jsp与servlet之间页面跳转及参数传递实例 分类: Java ...

  8. 前台页面Josn 数组在后台.cs代码中的解析

    后台代码: //解析jObject字符串 json_wang 2015/01/27 //var passengerListStr = BookingTicket_PassengerList_Hidde ...

  9. (一)问候Spring4

    第一节:Spring 简介 Spring 作者:Rod Johnson: 官方网站:http://spring.io/ 最新开发包及文档下载地址:http://repo.springsource.or ...

  10. 输出一个等边三角形的字母阵,等边三角形的两腰为字母A,往里靠依次字母大一个(详细题目文章中描述)

    题目简单的描述就是输出这么一个金字塔型的字母阵(等边三角形) /* A ABA ABCBA */ /* //解法①:对称轴法 #import <stdio.h> int main() { ...