题解

  1. 这里用的思路是:每次选择针对每个元素,每个元素都只有选与不选两种情况,所以共有2n种选择,并且这里将这些组合通过递归生成。
  2. 递归思路:这里的原问题即为solve(a, n, 0, m),即这2n种组合中是否有和为m的,其中的子问题就是solve(a, n, i, m),即在确定了前i个元素下其后面的元素能否组合出m。然后将solve(a, n, i, m)分治为:solve(a, n, i+1, m)和solve(a, n, i+1, m-a[i]),即每个子问题都将其分治为其下一个元素的选或不选,这样就可以递归实现了。
    1. 由于子问题共有n个,所以原问题在每个子问题的分治下被分成了2n个,类似二叉树型结构
    2. 这里通过i<=n即可对递归进行控制,防止其一直向下分治,当不满足条件时返回值或者不设置递归。
  3. 自己之前做这道题的思路是:每次选择针对所有元素,每次选择都要判断选择哪个元素。这样不但时间复杂度高,还需要对已选择的元素进行区别。只看到了表面的选择一组元素,没有发现选择的本质是每个元素的选与不选(类似二项分布中的组合)。

题目

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 ≤ 2000Sample Input 1
5
1 5 7 10 21
8
2 4 17 8 22 21 100 35Sample Output 1
no
no
yes
yes
yes
yes
no
noNotes
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 solve(int *a, int n, int i, int m)
{if(m==0)//从输入值m中减去所选元素,当m==0就说明和为m,搜索成功。return 1;if(i>=n)//i代表所选元素的个数,达到n就返回0,代表不成功return 0;int res = solve(a, n, i+1, m) || solve(a, n, i+1, m-a[i]);//模拟a中第i+1个元素的选与不选:左边的代表不选,右边的代表选。如果某种情况中有1就返回1,都是0就返回0。return res;
}int main(void)
{int i, n, q;cin>>n;int a[n];for(i=0; i<n; i++)cin>>a[i];cin>>q;for(i=0; i<q; i++){int m;cin>>m;if(solve(a, n, 0, m))cout<<"yes"<<endl;elsecout<<"no"<<endl;}return 0;
}

ALDS1_5_A: Exhaustive Search相关推荐

  1. ALDS1_5_A:Exhaustive search(穷举搜索)

    搜索问题 利 用 了 动 态 规 划 思 想 #include<iostream> #include<algorithm> #include<cstring> us ...

  2. Exhaustive Search Aizu - ALDS1_5_A

    Write a program which reads a sequence A of n elements and an integer M, and outputs "yes" ...

  3. Exhaustive search 和 Beam search 详解(图文并茂)

    1.Exhaustive search decoding Exhaustive search :也称为穷举法 我们理想的翻译序列 y 能够使如下条件概率最大 Exhaustive search 方法是 ...

  4. Brute force and exhaustive search

    Brute force and exhaustive search(蛮力和彻底搜索) Brute force is a straightforward approach to solving a pr ...

  5. AOJ——分治递归之Exhaustive Search穷尽搜索

    AOJ的分治递归的第一题: Exhaustive Search Write a program which reads a sequence A of n elements and an intege ...

  6. Exhaustive Search - 穷竭搜索

    方法: 1. 递归函数 2. 栈 3. 队列 4. 深度优先搜索( DFS , Depth-First Search),又常称为回溯法 5. 广度优先搜索(BFS, Breadth-First Sea ...

  7. weka: exhaustive search

    穷举搜索. 假设10个属性, 需要找出2^^10 种可能情形中, 那种的merit最优. 每次直接根据迭代次数space产生属性集 code: //best_group 初始为空 //best_mer ...

  8. Algorithms and Data Structures I

    Vintage 80s office by Mohamed Chahin 更好的阅读体验请访问 Algorithms and Data Structures I Acquire fundamental ...

  9. 组合搜索(combinatorial search)在算法求解中的应用

    1. 分治.动态规划的局限性 没有合适的分割方式时,就不能使用分治法: 没有合适的子问题或占用内存空间太大时,就不能用动态规划: 此时还需要回到最基本的穷举搜索算法. 穷举搜索(exhaustive ...

最新文章

  1. 【51CTO学院三周年】51cto学院的大数据培训之心路历程
  2. JS中的offsetWidth, clientWidth, scrollWidth, innerWidth, outerWidth, pageXOffset
  3. VMware vSphere: What’s New [V 5.1]
  4. 部分和模板的复杂嵌套
  5. Gradle Goodness: Set Java Compiler Encoding--转载
  6. 每天一道LeetCode-----将数字用字母表示(本质是26进制转换)
  7. 201571030316/201571030314 《小学四则运算练习软件需求说明》结对项目报告
  8. java cxf 不使用springmvc_使用cfx与springMVC集成发布与调用webservice
  9. IBM 、M$ 、Google Apple
  10. 梯度下降法与正规方程的比较
  11. discuz门户文章增加代码高亮
  12. 如何解决硬盘固件区损坏?只要学会这几步
  13. SAP License:浅析SAP智能云ERP – SAP S/4HANA Coud之美
  14. 超高精度UWB其实并不贵——UWB定位基站成本详解
  15. 如何修改CryEngine5.5着色器
  16. Uber是如何管理大规模数据工作流的?
  17. (字节流与字符流)InputStream字节输入流
  18. fetch和XHR的区别
  19. 网络安全等级保护与分级保护指导案例
  20. 万能word转换成pdf转换器下载

热门文章

  1. 协方差局长你_手把手教你协方差分析的SPSS操作!
  2. Large-scale image retrieval with attentive deep local features(DELF)阅读笔记
  3. mobaxterm怎么解除sessions个数限制_广发信用卡特定商户交易被限制怎么解决?多久解除?其实没那么复杂...
  4. 大一上学期总结(一)
  5. 生活哲理 (10月11日)
  6. python时间复杂度和空间复杂度_时间复杂度和空间复杂度
  7. 蓝桥杯实战应用【算法代码篇】-耐摔指数(附Python、Java和C++代码)
  8. 介绍Latex神器——Overleaf的方法
  9. c语言指数函数除了pow,用c语言写指数函数 C语言中的POW函数怎么使用
  10. 云软普创 API框架 简要说明