HDU 1560 DNA sequence A* 难度:1
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的更多相关文章
- HDU 1560 DNA sequence(DNA序列)
HDU 1560 DNA sequence(DNA序列) Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 1560 DNA sequence(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others) ...
- hdu 1560 DNA sequence(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 1560 DNA sequence (IDA* 迭代加深 搜索)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1560 BFS题解:http://www.cnblogs.com/crazyapple/p/321810 ...
- HDU 1560 DNA sequence(IDA*)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 题目大意:给出n个字符串,让你找一个字符串使得这n个字符串都是它的子串,求最小长度. 解题思路: ...
- HDU 1560 DNA sequence (迭代加深搜索)
The twenty-first century is a biology-technology developing century. We know that a gene is made of ...
- HDU - 1560 DNA sequence
给你最多8个长度不超过5的DNA系列,求一个包含所有系列的最短系列. 迭代加深的经典题.(虽然自己第一次写) 定一个长度搜下去,搜不出答案就加深大搜的限制,然后中间加一些玄学的减枝 //Twenty ...
- HDU 1560 DNA sequence DFS
题意:找到一个最短的串,使得所有给出的串是它的子序列,输出最短的串的长度,然后发现这个串最长是40 分析:从所给串的最长长度开始枚举,然后对于每个长度,暴力深搜,枚举当前位是哪一个字母,注意剪枝 注: ...
- POJ1699 HDU 1560 Best Sequence(AC自动机 最短路)
曾写过迭代加深搜索的方法,现在使用在AC自动上跑最短路的方法 dp[i][j]表示状态为到节点i,模式串是否包含的状态为j的最短串的长度,则状态转移方程为: dp[nx][ny] = min(dp[x ...
随机推荐
- Forbidden You don't have permission to access / on this server. You don't have permission to access /phpmyadmin/ on this server. 解决办法
Forbidden You don't have permission to access / on this server. 解决办法 打开 httpd.conf 文件, 将 # onli ...
- hdu 5476 Explore Track of Point(2015上海网络赛)
题目链接:hdu 5476 今天和队友们搞出3道水题后就一直卡在这儿了,唉,真惨啊……看着被一名一名地挤出晋级名次,确实很不好受,这道恶心的几何题被我们3个搞了3.4个小时,我想到一半时发现样例输出是 ...
- [js] 非常好的面试题,学习了。。
原文链接:http://www.cnblogs.com/xxcanghai/p/5189353.html
- Spring集成JPA提示Not an managed type
在做Spring与JPA集成时,出现问题如下: Caused by: java.lang.IllegalArgumentException: Not an managed type: class co ...
- C++ Primer 第三章 标准库类型string运算
1. 标准库类型 string string表示可变长的字符序列,使用string必须首先包含string头文件.如何初始化类的对象是由类本身决定的. int n; string s1;//默认初始化 ...
- iOS 打包静态类库 lib.a
iOS 打包静态类库 lib.a 流程 1: xcode---新建项目---iOS-framework&Library-----Cocoa touch Static Library----Ne ...
- GreenPlum简单性能测试与分析
版权声明:本文由黄辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/195 来源:腾云阁 https://www.qclou ...
- 图片流量节省大杀器:基于CDN的sharpP自适应图片技术实践
版权声明:本文由陈忱原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/156 来源:腾云阁 https://www.qclou ...
- 从别人那淘的知识 深入剖析Java中的装箱和拆箱
(转载的海子的博文 海子:http://www.cnblogs.com/dolphin0520/) 深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来 ...
- 如何使用JavaScript和正则表达式进行数据验证
利用客户端JavaScript的优势,JavaScript中的正则表达式可以简化数据验证的工作,下面与大家分享下如何使用JavaScript和正则表达式进行数据验证,感兴趣的朋友可以参考下哈 数据验证 ...