AOJ的分治递归的第一题:

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,搞了两天还是A不了,主要是穷举不完全,还有递归的终止条件比较模糊

看一下notes:

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].

一看就懂了,原来是这样确定递归条件啊,通过不断减去已列举的情况,再以最后的结果是否为零判断递归终止,其实想到了就非常简单(我还是太菜了)

看我的codes:

#include <iostream>
using namespace std;
int n;bool slove(int *A,int k, int m){if(m==0)return true;if(m<0||k==n)return false;return slove(A,k+1,m-A[k])||slove(A, k+1, m);}int main(){//    int n;cin>>n;int A[n];for(int i=0;i<n;i++)cin>>A[i];int q;cin>>q;int M[q];for(int i=0;i<q;i++){cin>>M[i];if((slove(A, 0, M[i]))==true)cout<<"yes"<<endl;elsecout<<"no"<<endl;}return 0;
}

总结:还是分解问题不够彻底,用其他的分解方法总是无法将这个问题分解完全。

AOJ——分治递归之Exhaustive Search穷尽搜索相关推荐

  1. ALDS1_5_A: Exhaustive Search

    题解 这里用的思路是:每次选择针对每个元素,每个元素都只有选与不选两种情况,所以共有2n种选择,并且这里将这些组合通过递归生成. 递归思路:这里的原问题即为solve(a, n, 0, m),即这2n ...

  2. Artificial Intelligence 人工智能 AI search AI 搜索

    文章目录 前言 一.Uninformed Search (无信息搜索) 二.Data structure for search tree 三.Breadth-first search (广度优先搜索) ...

  3. Brute force and exhaustive search

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

  4. 顺序表应用7:最大子段和之分治递归法

    Description 给定n(1<=n<=50000)个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的子段和的最 ...

  5. 调参必备--Grid Search网格搜索

    什么是Grid Search 网格搜索? Grid Search:一种调参手段:穷举搜索:在所有候选的参数选择中,通过循环遍历,尝试每一种可能性,表现最好的参数就是最终的结果.其原理就像是在数组里找最 ...

  6. c 最大子序列和_最大子序列和暴力法、分治+递归法、妙法

    你好,我是goldsunC 让我们一起进步吧! 最大子序列和 Question:给定整数(可能有负数),求的最大值(为方便起见,如果所有整数均为负数,则最大子序列和为0). 示例: IN : [-2, ...

  7. HDU1007 Quoit Design 分治+递归

    点击打开链接 Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. C++words search单词搜索的算法实现(附完整源码)

    C++words search单词搜索的算法实现 C++words search单词搜索的算法实现完整源码(定义,实现,main函数测试) C++words search单词搜索的算法实现完整源码(定 ...

  9. C++ternary search三元搜索的实现算法(附完整源码)

    C++ternary search三元搜索的实现算法 C++ternary search三元搜索的实现算法完整源码(定义,实现,main函数测试) C++ternary search三元搜索的实现算法 ...

最新文章

  1. linux下普通用户如何使用80端口启动程序
  2. 《多元统计分析》学习笔记之聚类分析
  3. java 十六进制浮点_Java十六进制浮点文字
  4. 脑洞大开的插画师,每幅都戳到我诡异的笑点
  5. hadoop没有datanode_Hadoop运行在Kubernetes平台实践
  6. android 页面整体滑动,ScrollView 实现页面整体滑动
  7. css-选择器-进阶-属性选择器-组选择器-nth选择器
  8. ei拼音的四个声调对应的字_非常容易混淆的汉语拼音“er”,究竟特殊在哪里?孩子总是搞不懂...
  9. 基于知识图谱的推荐系统综述
  10. 前端控制台返回406错误解决方法
  11. 神经网络与深度学习(第一章)(五)
  12. BRAF蛋白F595S G615R突变的影响
  13. eclipse如何设置背景颜色
  14. 2016.06工作日志
  15. WebRTC Trickle ICE 策略
  16. ES地理范围查询第一讲:Java操作地理位置信息(geo_point)
  17. 「解读」华为云桌面说“流畅”的时候,究竟在说什么?
  18. As I Began to Love Myself
  19. 解决 VSCode Ctrl Shift P 快捷键不可用
  20. AnyLogic第二讲行人仿真空间逻辑讲解

热门文章

  1. 用python模拟掷硬币_用计算机模拟掷硬币试验
  2. html的下拉列表框
  3. 代码随想录刷题|LeetCode 332.重新安排行程 51. N皇后 37. 解数独
  4. SQL server 是一门什么语言
  5. AVD模拟器怎么配置上网
  6. ROS 53端口 被狂扫
  7. Tableau绘制饼图和环形图
  8. 华为2018年校园招聘机试题
  9. 开源、易扩展、方便集成的Web绘图工具(流程图、架构图、组态、SCADA、大屏)
  10. mysql 5.5.48源码安装_centos5.5+apache2.2.15+mysql5.1.48+php5.2.13 编译安装 --by 画符抓鬼撰聊斋.doc...