【搜索进阶】HDU 1560 DNA sequence
来源:互联网 发布:电脑编程需要什么软件 编辑:程序博客网 时间:2024/05/16 09:16
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560
IDA*: 容易看出最少步数肯定是大于等于最大字符长度的,所以就构造 h() 为所有剩余长度的最大值
单广:开int进行hash,每组cas的hash值不同,这样就省去了每次初始化的消耗
IDA*: 容易看出最少步数肯定是大于等于最大字符长度的,所以就构造 h() 为所有剩余长度的最大值
#include<cstdio>#include<cstring>#include<bitset>using namespace std;#define MAX(a,b) (a>b?a:b)char code[]="ATGC";int pos[10],n;int h(){ int ans=0; for(int i=0;i<n;i++){ ans=MAX(pos[i],ans); } return ans;} int deep;char maze[10][8];bool dfs(int d){ int th=h(); if(th==0) return true; if(d+th>deep) return false; for(int i=0;i<4;i++){ bool flag=false; int tpos[10]; for(int j=0;j<n;j++){ tpos[j]=pos[j]; if(pos[j]==0) continue; if(maze[j][pos[j]-1]==code[i]){ pos[j]--; flag=true; } } if(flag){ if(dfs(d+1)) return true; } for(int j=0;j<n;j++){ pos[j]=tpos[j]; } } return false;} int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%s",maze[i]); pos[i]=strlen(maze[i]); } deep=0; while(true){ if(dfs(0)) break; deep++; } printf("%d\n",deep); } return 0;}
单广:开int进行hash,每组cas的hash值不同,这样就省去了每次初始化的消耗
#include "stdio.h"#include "string"#define M 1679616+100000struct H{ int st; int len;}q[M];int hash[M],cas =1;int n , end ,len;char str[8][8];char ku[] ="ACGT";int bfs() { int head,tail,i,st; int pos[8]; q[0].len =0; q[0].st =0; head = tail =0; while(head <= tail) { st = q[head].st; for(i =0 ; i < n ; i ++) { pos[i] = st%len; st/=len; } for(int j =0 ; j <4 ; j ++) { st =0; for(i = n-1; i >=0 ;i --) { st = st * len + pos[i] + (str[i][pos[i]] == ku[j]); } if(hash[st] == cas) { continue; } if(st == end) { return q[head].len +1; } hash[st] = cas; tail ++; q[tail].len = q[head].len +1; q[tail].st = st; } head ++; } return -1;}int main() { int T , i ; scanf("%d",&T); while(T --) { scanf("%d",&n); len =0; for(i =0 ; i < n ; i ++) { scanf("%s",str[i]); if(strlen(str[i]) > len) { len = strlen(str[i]); } } len ++; end =0; for(i = n-1 ; i >=0 ; i --) { end = end * len + strlen(str[i]); } printf("%d\n",bfs()); cas ++; } return 0;}
0 0
- 【搜索进阶】HDU 1560 DNA sequence
- HDU 1560 DNA sequence IDA*搜索
- HDU 1560 DNA sequence(IDA*搜索)
- HDU 1560 DNA sequence(状压+搜索)
- HDU - 1560 DNA sequence
- HDU-1560 DNA sequence
- HDU 1560DNA sequence
- HDU 1560 - DNA sequence
- HDU - 1560----DNA sequence
- HDU - 1560----DNA sequence
- hdu 1560 DNA sequence(迭代加深搜索)
- hdu 1560 DNA sequence(迭代加深搜索)(经典题)
- hdu 1560 DNA sequence(迭代加深搜索)
- HDU 1560 DNA sequence IDA*
- hdu 1560 DNA sequence(IDA*)
- HDU 1560 DNA sequence(IDA*~)
- HDU 1560 DNA sequence hh大神代码
- hdu 1560 DNA sequence (IDA*)
- 考研英语自我介绍
- FileReader,FileWriter,BufferedReader,BufferedWriter,File,FilenameFilter,PrintWriter,Seque)
- Python OS模块学习(一)
- 包装设计模式设计自己的BufferedReader方法,解密其底层
- 安装ubuntu后 配置C开发环境
- 【搜索进阶】HDU 1560 DNA sequence
- 国际制单位词头表
- Cassandra使用pycassa批量导入数据
- LeetCode 33 Pascal's Triangle II
- PHP发送邮件
- Machine Learning—Mixtures of Gaussians and the EM algorithm
- 支持向量机SVM(二)
- 攻城狮生活
- 在cxf中使用配置避免增加字段导致客户端必须更新的问题