题目描述: Subset Sum
Tags: 回溯
子集和问题的一个实例为〈 S,t 〉。其中,S={x1 ,x2 ,…, xn }是一个正整数的集合,c是一个正整数。子集和问题判定是否存在S的一个子集S1,使得x1+x2+...+xk=S, 其中x1,x2...xk属于集合S1。 对于给定的正整数的集合S和正整数c,编程计算S 的一个子集S1,使得x1+x2+...+xk=S, 其中x1,x2...xk属于集合S1。

输入

第1 行有2 个正整数n 和c,n 表示S 的大小,c是子集和的目标值。接下来的1 行中,有n 个正整数,表示集合S 中的元素。

输出

子集和问题的解。当问题无解时,输出“No Solution!”。

样例输入

5 10
2 2 6 5 4

样例输出

2 2 6

思路:DFS,找一个子集树就可以了,但是总是PE。。。。。这个就很头痛。。。。。
 // subset sum.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int MAX = ;
int n, c, sign, arr[MAX], vis[MAX], sum[MAX]; void print()
{
for (int i = ; i<n; i++)
if (vis[i]) cout << arr[i] << " ";
cout << endl;
} //搜索的位置,目前的和
void DFS(int pos, int cur)
{
//cout << "pos:" << pos << "\tcur:" << cur << endl;
if (sign) return; //找到一组就可以直接结束搜索过程
if (cur == c)
{
sign = ; print();
return;
} //加个特殊判断,cur+余下<c 不可能凑够c
if (pos >= n || cur > c || cur + sum[n-] - sum[pos-] < c ) return; vis[pos] = ;//选择
DFS(pos + , cur + arr[pos]); vis[pos] = ;//不选择
DFS(pos + , cur); } int main()
{
while (cin >> n >> c)
{
sign = ;
memset(vis, , sizeof(vis));
memset(sum, , sizeof(sum)); for (int i = ; i < n; i++)
{
cin >> arr[i];
sum[i] = sum[i-] + arr[i]; //减枝!!判断剩下的所有的数字是否能够合成c
}
if (sum[n-] < c)//很重要的剪枝!!如果所有的数加起来都小于c,那么不可能有解。。之前有三组TLE,加了这一步竟然给蒙过了。。
{
//cout << "sum[n - 1]:"<<sum[n - 1] << endl;
cout << "No Solution!";
}
else
{
DFS(, );
if (sign == ) cout << "No Solution!"<<endl;
} } return ;
}

ACM-Subset sum的更多相关文章

  1. Subset sum problem

    https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...

  2. 动态规划法(三)子集和问题(Subset sum problem)

      继续讲故事~~   上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界.   这天,丁丁刚回到家,他 ...

  3. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  4. LN : leetcode 416 Partition Equal Subset Sum

    lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...

  5. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

  6. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  7. Leetcode 416. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  10. 416. Partition Equal Subset Sum

    题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...

随机推荐

  1. mybatis查询时间段

    参考:https://bbs.csdn.net/topics/391838987 <!-- 查询条件:创建开始时间 --> <if test="createdBeginti ...

  2. 使用EA画流程图

    https://www.sparxsystems.com.au/enterprise_architect_user_guide/13.0/model_domains/flow_chart.html

  3. LATTICE 编程烧录器HW-USBN-2B使用说明

    HW-USBN-2B说明文档 1.       引脚定义 编程引脚 名称 编程设备引脚类型 描述 VCC 编程电压 输入 连接VCC到目标设备,典型的ICC=10Ma.板子设计必须考虑VCC的电流供应 ...

  4. 常用命令提示符(cmd)

    MS-DOS(Microsoft Disk Operation System)命令提示符(cmd) 启动: win+ R 输入cmd回车切换盘符   盘符名称:进入文件夹   cd 文件夹名称进入多级 ...

  5. 【高软作业2】:Java IDE调研分析

    一 序言 随着软件项目开发需求的增多,拥有一款优秀的.顺手的IDE(Integrated Development Environment)对程序员来说显得格外重要.本文就Java程序开发,选择了3款I ...

  6. LNMP 常见问题(FAQ)

    常见问题(FAQ)常见问题关键词快速索引 我们为什么需要采用LNMP架构?原因不在重复,请看:关于 LNMP一键安装包支持哪些Linux发行版?目前支持CentOS(RadHat).Debian.Ub ...

  7. loadrunner controller查看参数日志

    1.在generator,我们用这个语句来输出参数:lr_output_message("目录为%s", lr_eval_string("{NewParam}" ...

  8. Tensorflow官方文档 input_data.py 下载

    说明: 本篇文章适用于MNIST教程下载数据集. # Copyright 2015 Google Inc. All Rights Reserved. # # Licensed under the Ap ...

  9. Nginx配置的一些说明(添加https证书)

    server { listen 443 ssl; #监听https 443端口 server_name www.XXXX.com; client_max_body_size 260M; #这下面的就是 ...

  10. 第1节 IMPALA:1、impala的基本介绍

    impala的介绍: impala是cloudera公司开源提供的一款高效率的sql查询工具 impala可以兼容hive的绝大多数的语法,可以完全的替代表hive impala与hive的关系:紧耦 ...