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 ...
随机推荐
- Codeforces Round #648 (Div. 2) A. Matrix Game
题目链接:https://codeforces.com/contest/1365/problem/A 题意 给出一个 $n \times m$ 的网格,两人轮流选择一个所在行列没有 $1$ 的方块置为 ...
- 【uva 11054】Wine trading in Gergovia(算法效率--等价转换)
题意:N个等距村庄,买(>0)卖(<0)酒,供需平衡,运K则需K劳动力.问所需的最小劳动力. 解法:由于运出或运入1的都需经过2,所以无论如何,都可以等价于从2本身运入或运出.因此可以将1 ...
- A. Little Pony and Expected Maximum
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she ...
- 【2020杭电多校】 Lead of Wisdom、The Oculus
题目链接:Lead of Wisdom 题意:有n个物品,这些物品有k种类型.每种物品有对应的类型ti,其他值ai,bi,ci,di 你可以选择一些物品,但是这些物品要保证它们任意两者之间类型不能相同 ...
- java——字符串常量池、字符串函数以及static关键字的使用、数组的一些操作函数、math函数
字符串常量池: 字符串比较函数: 字符串常用方法: 字符串截取函数: 字符串截取函数: static关键字使用: 要调用类中的static类型的变量的时候,可以用"类名.变量名&quo ...
- fzu2204 7
Problem Description n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个. Input 第一行有多组数据.第一行T表 ...
- Codeforces Round #653 (Div. 3) C. Move Brackets
题意/题解:经典括号匹配题目,不多说了. 代码: int t; int n; string s; int cnt; int main() { ios::sync_with_stdio(false);c ...
- kubernetes实战-交付dubbo服务到k8s集群(二)交付jenkins到k8s集群
首先下载jenkins镜像并上传到我们自己的私有仓库:7-200 # docker pull jenkins/jenkins:2.190.3 # docker tag 22b8b9a84dbe har ...
- Internationalization API & ECMA-402
Internationalization API & ECMA-402 i18n https://caniuse.com/?search=Internationalization API In ...
- APP 金刚区图标设计 & UI
APP 金刚区图标设计 & UI https://www.zcool.com.cn/article/ZNzk4Njg0.html