HDOJ-1560(迭代加深搜索问题)
DNA sequence
HDOJ-1560
*本题是迭代加深搜索问题,主要是要理解题目,题目中一定是有解的,所以为了找最小的解,可以从小的搜索深度开始逐渐增加。
*这里有个技巧就是,如果本次指定开始迭代搜索的深度之后没有找到解,那么需要用一个临时数组将每个字符串中已经匹配好的字符数存起来。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int n;
char chain[8][5];
int p[8];//表示第i个字符串已经匹配好的字符数
int charNums[8];//表示第i个字符串的长度
bool flag=false;
char dna[4]={'A','C','G','T'};
int remainChars(){
int maxs=0;
for(int i=0;i<n;i++){
maxs=max(maxs,charNums[i]-p[i]);
}
return maxs;
}
void dfs(int depth){
if(flag)
return;
if(remainChars()==0){//所有字符串中带匹配的字符数都为0个的话,则搜索结果找到了
flag=true;
return;
}
if(remainChars()>depth)
return;
int temp[8];
for(int i=0;i<n;i++){//开一个辅助数组,先将每个字符串已经匹配好的字符数存起来,如果后序没有找到正确结果再复制回去
temp[i]=p[i];
}
for(int j=0;j<4;j++){
bool find=false;
for(int i=0;i<n;i++){
if(chain[i][p[i]]==dna[j]){
p[i]++;//对于每一个属于dna的字符,对于每个n个字符串中待匹配位置的字符和它相符,待匹配位置都加一
find=true;
}
}
if(find){
dfs(depth-1);
if(flag){//如果递归回来以后发现找到了,则
return;
}
for(int i=0;i<n;i++){
p[i]=temp[i];
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--){
cin>>n;
int maxs=0;//表示初始时就需要传入迭代搜索里面的最小搜索深度
flag=false;
memset(p,0,sizeof(p));
//memset(chain,'\0',sizeof(chain));//
for(int i=0;i<n;i++){
cin>>chain[i];
int len=strlen(chain[i]);
charNums[i]=len;
maxs=max(maxs,len);
}
while(1){//对于每个循环,每次迭代深度都增加一,知道找到结果。
dfs(maxs);
if(flag){
cout<<maxs<<endl;
break;
}
maxs++;
}
}
//system("pause");
return 0;
}
HDOJ-1560(迭代加深搜索问题)的更多相关文章
- 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(迭代加深搜索)
DNA sequence Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU 1560 DNA sequence (迭代加深搜索)
The twenty-first century is a biology-technology developing century. We know that a gene is made of ...
- POJ1129Channel Allocation[迭代加深搜索 四色定理]
Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14601 Accepted: 74 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 迭代加深搜索 POJ 1129 Channel Allocation
POJ 1129 Channel Allocation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14191 Acc ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- 迭代加深搜索 C++解题报告 :[SCOI2005]骑士精神
题目 此题根据题目可知是迭代加深搜索. 首先应该枚举空格的位置,让空格像一个马一样移动. 但迭代加深搜索之后时间复杂度还是非常的高,根本过不了题. 感觉也想不出什么减枝,于是便要用到了乐观估计函数(O ...
- C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains
此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...
随机推荐
- KMP(算法描述)
#include<iostream> using namespace std; const int N=10010,M=100010; int n,m; char p[N],s[M]; i ...
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- 说说Golang goroutine并发那些事儿
摘要:今天我们一起盘点一下Golang并发那些事儿. Golang.Golang.Golang 真的够浪,今天我们一起盘点一下Golang并发那些事儿,准确来说是goroutine,关于多线程并发,咱 ...
- cmder设置方法
一.添加鼠标右键 Cmder.exe /REGISTER ALL 二.添加系统环境变量 我的电脑 > 右键属性 > 高级系统设置 > 环境变量 > 系统变量,在path中添加 ...
- Apple WWDC All In One
Apple WWDC All In One https://developer.apple.com/wwdc20/ https://developer.apple.com/videos/wwdc202 ...
- Jupyter All In One
Jupyter All In One Jupyter Architecture https://jupyter.readthedocs.io/en/latest/projects/architectu ...
- js & object & prototype & __proto__ & prototype chain
js & object & prototype & proto & prototype chain constructor prototype === instance ...
- js repeatify & no for loop
js repeatify & no for loop js repeatify https://www.sitepoint.com/5-typical-javascript-interview ...
- ORM & sequelize
ORM Object Relational Mapping 对象关系映射 Table => Object, 简化 SQL 查询命令的编写 https://en.wikipedia.org/wik ...
- Dart 中断Future
更多 中断future 方法1) import 'package:async/async.dart'; void main() { var get = CancelableOperation.from ...