Exhaustive Search Aizu - ALDS1_5_A
Write a program which reads a sequence A of n elements and an integer M, and outputs “yes” if you can make M by adding elements in A, otherwise “no”. You can use an element only once.
You are given the sequence A and q questions where each question contains Mi.
Input
In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers (Mi) are given.
Output
For each question Mi, print yes or no.
Constraints
n ≤ 20
q ≤ 200
1 ≤ elements in A ≤ 2000
1 ≤ Mi ≤ 2000
Sample Input 1
5
1 5 7 10 21
8
2 4 17 8 22 21 100 35
Sample Output 1
no
no
yes
yes
yes
yes
no
no
Notes
You can solve this problem by a Burte Force approach. Suppose solve(p, t) is a function which checkes whether you can make t by selecting elements after p-th element (inclusive). Then you can recursively call the following functions:
solve(0, M)
solve(1, M-{sum created from elements before 1st element})
solve(2, M-{sum created from elements before 2nd element})
…
The recursive function has two choices: you selected p-th element and not. So, you can check solve(p+1, t-A[p]) and solve(p+1, t) in solve(p, t) to check the all combinations.
For example, the following figure shows that 8 can be made by A[0] + A[2].
思路
设solve(i,m)为“用第i个元素后面的元素能得出m时返回true”的函数,这样一来solve(i,m)就可以分解为solve(i+1,m)和solve(i,m-A[i])这两个更小的局部问题。
函数solve(i,m)中,m==0时代表数组元素相加能够得出指定整数。相反,m>0且i>=n时表示数组元素相加得不出指定整数。
只要局部问题solve(i+1,m)和solve(i,m-A[i])之中有一个为true,原问题solve(i,m)就为true。
code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 ^ 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^ 0 ^^110.^0 0 ^ ^^^10.01^^ 10 1 1 ^^^1110.101 10 1.1 ^^^1111110010 01 ^^ ^^^1111^1.^ ^^^10 10^ 0^ 1 ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/
// Virtual_Judge —— Exhaustive Search Aizu - ALDS1_5_A.cpp created by VB_KoKing on 2019-05-04:12.
/* Procedural objectives:Variables required by the program:Procedural thinking:Functions required by the program:*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first gentle breeze that passed through my ear is you,
The first star I see is also you.
The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!!
*/
#include <iostream>
using namespace std;int n,A[50];//从输入值M中减去所选元素的递归函数
int solve(int i,int m)
{if (m==0) return 1;if (i>=n) return 0;return solve(i+1,m)+solve(i+1,m-A[i]);
}int main()
{int q,M;cin>>n;for (int i = 0; i < n; i++)cin>>A[i];cin>>q;for (int i = 0; i < q; i++) {cin>>M;if (solve(0,M))cout<<"yes"<<endl;elsecout<<"no"<<endl;}return 0;
}
Exhaustive Search Aizu - ALDS1_5_A相关推荐
- ALDS1_5_A: Exhaustive Search
题解 这里用的思路是:每次选择针对每个元素,每个元素都只有选与不选两种情况,所以共有2n种选择,并且这里将这些组合通过递归生成. 递归思路:这里的原问题即为solve(a, n, 0, m),即这2n ...
- Exhaustive search 和 Beam search 详解(图文并茂)
1.Exhaustive search decoding Exhaustive search :也称为穷举法 我们理想的翻译序列 y 能够使如下条件概率最大 Exhaustive search 方法是 ...
- Brute force and exhaustive search
Brute force and exhaustive search(蛮力和彻底搜索) Brute force is a straightforward approach to solving a pr ...
- AOJ——分治递归之Exhaustive Search穷尽搜索
AOJ的分治递归的第一题: Exhaustive Search Write a program which reads a sequence A of n elements and an intege ...
- Exhaustive Serch ( Aizu - ALDS1_5_A )
题目链接 :https://vjudge.net/problem/Aizu-ALDS1_5_A 穷举搜索 1 #include<stdio.h> 2 3 int n, A[50]; 4 5 ...
- ALDS1_5_A:Exhaustive search(穷举搜索)
搜索问题 利 用 了 动 态 规 划 思 想 #include<iostream> #include<algorithm> #include<cstring> us ...
- Exhaustive Search - 穷竭搜索
方法: 1. 递归函数 2. 栈 3. 队列 4. 深度优先搜索( DFS , Depth-First Search),又常称为回溯法 5. 广度优先搜索(BFS, Breadth-First Sea ...
- weka: exhaustive search
穷举搜索. 假设10个属性, 需要找出2^^10 种可能情形中, 那种的merit最优. 每次直接根据迭代次数space产生属性集 code: //best_group 初始为空 //best_mer ...
- 组合搜索(combinatorial search)在算法求解中的应用
1. 分治.动态规划的局限性 没有合适的分割方式时,就不能使用分治法: 没有合适的子问题或占用内存空间太大时,就不能用动态规划: 此时还需要回到最基本的穷举搜索算法. 穷举搜索(exhaustive ...
最新文章
- 2022-2028年中国氢化丁腈橡胶行业市场深度分析及投资规模预测报告
- Java8 Stream应用:Map合并、过滤、遍历、values int求和等
- 将CVESUMMARY写成HTML文件
- mac上使用crontab周期性执行python脚本
- 石墨烯可将硬盘容量提高十倍,剑桥在Nature子刊发表最新研究
- 读《活着》----余华
- python 字典查询比列表快_Python 字典和列表的对比应用
- 一个拖拽的效果类和dom-drag.js
- 简单绑定要注意的问题_AX
- postman websocket_postman的“替代者”postwoman的使用体验—从入门到放弃
- c语言 word转pdf,批量Word转PDF之捷径
- 微博舆情挖掘需求分析
- PE装机工具-U深度制作
- WordPress 安装时常见的数据库的错误
- 控制算法简析1——PID和负反馈的数学原理
- 农庄规划软件测试,《模拟农场17》游戏评测:现代化农场让你学会如何种田
- html5清除所有,html5 canvas永久清除
- c蔚语言艺术,晚唐张乔诗歌的语言艺术与美学风格-中国社会科学网.PDF
- win7常用工具软件记录之Clover(附加下载地址)
- 主流数据库的默认隔离级别
热门文章
- linux文件属性权限相关
- H.264 Quantization
- kdevelp 导入makefile工程
- UML小结以及基于领域模型的系统设计初步
- 批量处理jdbc语句提高处理速度
- Ajax联动下拉框的实现例子
- 怎么判断手机在抖动_集合来了!激光头切割过程中一直抖动、跳动、上下动是什么原因?...
- Java黑皮书课后题第1章:1.1(显示三条消息)编写程序,显示Welcome to Java、Welcome to Computer Science和Programming is fun
- MYSQL学习01--MySQL基础知识
- .net使用SqlBulkCopy类操作DataTable批量插入数据库数据,然后分页查询坑