题目描述: 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. docker的概念

    Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Lin ...

  2. Android中利用正则表达式验证手机号是否合法

    利用正则表达式来验证手机号是否合法,现在我们简单使用一下,这个在实际的应用中很实用. 例:在输入框中输入手机号,判断是否合法, MainActivity中: package com.example.m ...

  3. 前端学习笔记系列一:6 一种新的css预编译器stylus

    stylus是 CSS 的预处理框架.CSS 预处理,顾名思义,预先处理 CSS.那 stylus 咋预先处理呢?stylus 给 CSS 添加了可编程的特性,也就是说,在 stylus 中可以使用变 ...

  4. 记一次菜鸡的低级折腾--WordPress get Webshell(后台文件编辑插马)

    挺简单的一个测试站,开始思路错了,一直去网上找WordPress的漏洞,看有没有什么能利用的,未果,因为这个测试站有些地方并不完善,有的漏洞利用不了,菜鸡的我连弱口令都没猜对,没知识就是这么悲哀. 下 ...

  5. loadrunner 手动添加关联

    以loadrunner自带的订票系统为例 1.录制两次订票流程 2.比较两次不同点(Tools ->Compare with script) 3.找到服务器返回的动态值 4.回到Script模式 ...

  6. Windows驱动开发-DeviceIoControl函数参数dwIoControlCode

    函数语法 BOOL DeviceIoControl( HANDLE hDevice, DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBuffer ...

  7. 51nod 1433:0和5

    1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  取消关注 小K手中有n张牌,每张牌上有一个一位数的数, ...

  8. esxi 版本升级命令

    先把zip文件通过XShell或者WinSCP上传到esxi服务器上面去,然后执行以下命令,完成升级并重启就可以了.

  9. Web项目设置编码格式

    1. 如果用的Tomcat ,请修改server.xml 中Connector URIEncoding="UTF-8" (GET请求时) 2. 用字符编码过滤器,设置默认编码方式为 ...

  10. Redis——从入门到放弃

    redis简介 Redis is an open source (BSD licensed), in-memory data structure store, used as a database, ...