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. poj2354Titanic(两点的球面距离)

    链接 球面距离计算公式:d(x1,y1,x2,y2)=r*arccos(sin(x1)*sin(x2)+cos(x1)*cos(x2)*cos(y1-y2)) x1,y1是纬度\经度的弧度单位,r为地 ...

  2. numpy库的常用知识

    为什么有numpy这个库呢?准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2, ...

  3. hibernate的离线关联(多级)查询

    如果实体对象中没有关联对象的情况使用DetachedCriteria进行查询是一件很简单的事情. 假设要通过stuName查询一个学生Student记录,可以如下: Java代码 DetachedCr ...

  4. Android控件之MultiAutoCompleteTextView(自动匹配输入的内容)

    一.功能 可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配,可用在发送短信,发邮件时选择联系人这种类型中 二.独特属性 android:comp ...

  5. Jq_Ajax 操作函数跟JQuery 遍历函数跟JQuery数据操作函数

    JQuery文档操作方法 jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 函数                             ...

  6. hiho_1081_最短路径1

    题目 最短路模板题目,纯练习手速. 实现 #include<iostream> #include<string.h> #include<iostream> #inc ...

  7. Lua 栈中元素的位置

    Lua与C.C#等的交互是通过栈来实现的,每次插入元素都是放在栈顶(top),至于元素的index,可以使用正数和负数两种方式, 如取栈底开始至第index个元素 -index = gettop - ...

  8. VB6 GDI+ 入门教程[3] 笔、刷子、矩形、椭圆绘制

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[3] 笔.刷子.矩形.椭圆绘制 2009 年 6 月 1 ...

  9. VB6 GDI+ 入门教程[8] Bitmap魔法(1):创建

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[8] Bitmap魔法(1):创建 2009 年 9 月 ...

  10. VC++编译GSL

    目录 第1章 VC++    1 1.1 修改行结束符    1 1.2 修改#include "*.c" 为 #include "*.inl"    2 1. ...