Exhaustive Search
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].
#include <iostream>
using namespace std; int n, q;
int A[2010], M[2010]; // 用第i个元素后面的元素能得出m时返回true
// 从输入值M中减去所选元素的递归函数
bool solve(int i, int m)
{
if(m == 0) return true;
if(i >= n) return false;
return solve(i + 1, m) || solve(i + 1, m - A[i]); // 不使用第i个元素 + 使用第i个元素
} int main()
{
cin >> n;
for(int i = 0; i < n; ++ i)
{
cin >> A[i];
}
cin >> q;
for(int i = 0; i < q; ++ i)
{
cin >> M[i];
if(solve(0, M[i]))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
} return 0;
} /*
5
1 5 7 10 21
8
2 4 17 8 22 21 100 35
*/

Exhaustive Search的更多相关文章
- 3.2. Grid Search: Searching for estimator parameters
3.2. Grid Search: Searching for estimator parameters Parameters that are not directly learnt within ...
- 论文笔记:Selective Search for Object Recognition
与 Selective Search 初次见面是在著名的物体检测论文 「Rich feature hierarchies for accurate object detection and seman ...
- 目标检测--Selective Search for Object Recognition(IJCV, 2013)
Selective Search for Object Recognition 作者: J. R. R. Uijlings, K. E. A. van de Sande, T. Gevers, A. ...
- Selective Search for Object Recognition
http://blog.csdn.net/charwing/article/details/27180421 Selective Search for Object Recognition 是J.R. ...
- 组合搜索(combinatorial search)在算法求解中的应用
1. 分治.动态规划的局限性 没有合适的分割方式时,就不能使用分治法: 没有合适的子问题或占用内存空间太大时,就不能用动态规划: 此时还需要回到最基本的穷举搜索算法. 穷举搜索(exhaustive ...
- RCNN,Fast RCNN,Faster RCNN 的前生今世:(1) Selective Search
Selective Search for Object Recoginition 这篇论文是J.R.R. Uijlings发表在2012 IJCV上的一篇文章,主要介绍了选择性搜索(Selective ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读3
Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong 在前 ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读2
Selective Search for Object Recognition 是J.R.R. Uijlings发表在2012 IJCV上的一篇文章.主要介绍了选择性搜索(Selective Sear ...
- 【计算机视觉】Selective Search for Object Recognition论文阅读1
Selective Search for Object Recognition 作者: J. R. R. Uijlings, K. E. A. van de Sande, T. Gevers, A. ...
随机推荐
- vue中添加swiper轮播插件
网上找了很多,最后还是官网最完整. https://github.com/surmon-china/vue-awesome-swiper 安装: 1.npm install vue-awesome-s ...
- PowerShell 惠普打印机双面驱动自动设置已安装
win10系统,使用实验室的HP P2055dn打印机.每次关机重连后,都会把默认的双面打印机的设置改回“未安装”,需要手动改成“已安装”.感觉是个bug,win7的时候关机后状态还会保持. 每次连上 ...
- java读取txt文件,对字符串进行操作后导出txt文件
嘿嘿,代码略为简单,不再多做解释,直接上码! package org.lq.com.util; import java.io.File; import java.io.InputStreamReade ...
- Effective C++ .13使用智能指针来引用资源
#include <iostream> #include <cstdlib> #include <memory> using namespace std; clas ...
- csharp: Getting all image files in folder
/// <summary> /// /// </summary> /// <param name="sender"></param> ...
- 51Nod1957 有限背包计数问题
传送门 另一个传送门 这题还挺有意思…… 先贴一波出题人的题解…… (啥你说你看不见?看来你还没过啊,等着A了再看或者乖乖花点头盾好了……) 然后是我的做法……思想都是一样的,只是细节不一样而已…… ...
- react+antd 选项卡切换
index.js: import React from 'react'; import ReactDOM from 'react-dom'; import CardSecond from './Car ...
- 03_Zookeeper基本数据模型及基本命令操作
[Zookeeper基本数据模型及注意点] * zk的数据模型可以类比为Linux的文件目录,是一种树状结构,如:/dubbo/com.service.DemoService/provider.... ...
- Android MediaRecorder实现暂停断点录音功能
基本原理如下:MediaRecorder通过MIC录音,系统没有自带的pause功能,每次暂停录音,都会结束本次的录音.现在本人的设计思路是:MediaRecorder录音暂停时,保存这段所录下的音频 ...
- How to import Django DB operations out of Django projects
i am not familiar with DB opertions. usually i stroe data to txt and other formats. as DB is more an ...