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

仔细读题(!),则可发现这道题要求的是一个最短的字符串,该字符串的不连续子序列中包含题目所给的所有字符串

因为总共只有40个字符,可以尝试使用A*搜索

1.存储状态时直接存储40个字符,每个字符4种可能是肯定不行的.

因为要求的是包含不连续的子序列,所以只需记住当前字符串长度与每个子序列已经包含在当前字符串的长度,

比如题目中的输入样例

4

ACGT

ATGC

CGTT

CAGT

可以这样存储一个序列

ATG:len=3,s[0]=1,s[1]=3,s[2]=0,s[3]=0,

ATC:len=3,s[0]=2,a[1]=2,s[2]=1,s[3]=1,

又因为只有8个子序列,每个子序列长度不超过5,也就是说可以采用6进制来压缩状态数组.总共不超过6^9=10077696种状态,空间时间都满足

2.评估函数随便选取了当前未实现的最长长度,2483ms过关

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxsta=10077696;
char str[8][6];
int l[8];
int grade[8][6];
bool vis[maxsta];
int s[8];
int n;
struct status{
int len,f;
int sta;
status():len(0),f(0),sta(0){}
status(int _len,int _f,int _sta):\
len(_len),f(_f),sta(_sta){}
bool operator <(status s2)const {
if(s2.len!=len)return len>s2.len;
return f>s2.f;
}
void tos(){
int tsta=sta;
for(int i=n-1;i>=0;i--){
s[i]=sta%6;
sta/=6;
}
sta=tsta;
}
static int tosta(){
int sta=0;
for(int i=0;i<n;i++){
sta*=6;
sta+=s[i];
}
return sta;
}
static int calcf(int sta){
int tmp[8];
int ans=0;
for(int i=n-1;i>=0;i--){
tmp[i]=sta%6;
sta/=6;
ans=max(ans,l[i]-tmp[i]);
}
return ans;
}
};
priority_queue<status> que;
int ed;
int bfs(){
while(!que.empty())que.pop();
status st=status(0,status::calcf(0),0);
char ch[4]={'A','G','C','T'};
que.push(st);
vis[0]=true;
while(!que.empty()){
status tp=que.top();que.pop();
if(tp.sta==ed)return tp.len;
for(int i=0;i<4;i++){
tp.tos();
for(int j=0;j<n;j++){
if(ch[i]==str[j][s[j]]){
s[j]++;
}
}
int tmpsta=status::tosta();
if(vis[tmpsta])continue;
vis[tmpsta]=true;
if(tmpsta==ed)return tp.len+1;
que.push(status(tp.len+1,status::calcf(tmpsta),tmpsta)); } }
return -1;
} int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++){
scanf("%s",str[i]);
l[i]=strlen(str[i]);
s[i]=l[i];
}
ed=status::tosta();
int ans=bfs();
printf("%d\n",ans);
}
return 0;
}

HDU 1560 DNA sequence A* 难度:1的更多相关文章

  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(搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others)  ...

  3. hdu 1560 DNA sequence(迭代加深搜索)

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

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

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

  5. HDU 1560 DNA sequence(IDA*)

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

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

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

  7. HDU - 1560 DNA sequence

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

  8. HDU 1560 DNA sequence DFS

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

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

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

随机推荐

  1. Git学习(2)Git 安装

    Windows 平台上安装 在 Windows 平台上安装 Git 同样轻松,有个叫做 msysGit 的项目提供了安装包,可以到 GitHub 的页面上下载 exe 安装文件并运行: 安装包下载地址 ...

  2. 使用连接来代替in和not in(使用外连接技巧)

    比如:表A里面的一个字段叫做MOBILE 里面存的记录如下 : 123456781 表B里面的一个字段也叫做MOBILE里面存的记录如下 12341910   (1)我们要查询一下A和B里面都有的,以 ...

  3. 疯狂java讲义之流程控制与数组

    while package ch4; /** * Created by Jiqing on 2016/11/6. */ public class While { public static void ...

  4. c++中vector的学习

    根据各种做题,发现数组并不是很适用于各种情况,当涉及到内存占用的时候,数组可能就没有vector的优势了,而vector,动态数组,比较适合某些情况. 接下来看看比较基本的vector用法: 1 #i ...

  5. 在Windows和Linux上安装paramiko模块

    一.paramiko模块有什么用? paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.由于使用的是python这样的能够跨平台运行的语言 ...

  6. foundation系列

    1如何将布尔值转为OC对象?  1把 BOOL 值包装到 NSNumber中: NSNumber *boolNumber = [NSNumber numberWithBool:YES]  2获取BOO ...

  7. nosql简述

    1.NoSQL数据库概念 NoSQL数据库是非关系型数据库,主要是针对关系型数据库而言,它主要是用来解决半结构化数据和非机构化数据的存储问题. 2.为什么使用NoSQL数据库? (1)对数据库的高并发 ...

  8. python 集合

    面向对象的集合: #coding:utf-8 __author__ = 'similarface' class Set: ''' list实现集合,及其集合操作 ''' def __init__(se ...

  9. 线性表 - 从零开始实现by C++

    参考链接:数据结构探险之线性表篇     线性表

  10. dedecms 网站优化技巧

    1.把列表文件中的<title>***</title>改为栏目名称-seo标题-网站名称即<title>{dede:field.title/}-{dede:fiel ...