ACM-Subset sum
输入
第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的更多相关文章
- Subset sum problem
https://en.wikipedia.org/wiki/Subset_sum_problem In computer science, the subset sum problem is an i ...
- 动态规划法(三)子集和问题(Subset sum problem)
继续讲故事~~ 上次讲到我们的主人公丁丁,用神奇的动态规划法解决了杂货店老板的两个找零钱问题,得到了老板的肯定.之后,他就决心去大城市闯荡了,看一看外面更大的世界. 这天,丁丁刚回到家,他 ...
- Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- LN : leetcode 416 Partition Equal Subset Sum
lc 416 Partition Equal Subset Sum 416 Partition Equal Subset Sum Given a non-empty array containing ...
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- Leetcode 416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] 416. Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
- 416. Partition Equal Subset Sum
题目: Given a non-empty array containing only positive integers, find if the array can be partitioned ...
随机推荐
- Python递归函数如何写?正确的Python递归函数用法!
在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.一.举个例子,我们来计算阶乘n! = 1 x 2 x 3 x … x n,用函数fact(n)表示,可以看出:fac ...
- NO27 定时任务
linux定时任务的设置 为当前用户创建cron服务 1. 键入 crontab -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/a ...
- 小程序列表循环出来的list是不同接口赋的值
需求:首页有三个列表,样式形式都是一样的,可以循环展示,但是循环的内容list部分是来自于不同的三个接口. data: { indexList:[{ name: "中考体能突击营" ...
- HiBench成长笔记——(9) Centos安装Maven
Maven的下载地址是:http://maven.apache.org/download.cgi 安装Maven非常简单,只需要将下载的压缩文件解压就可以了. cd /home/cf/app wget ...
- oracle的存储过程和函数有什么区别?
Oracle中的函数与存储过程的区别: A:函数必须有返回值,而过程没有. B:函数可以单独执行.而过程必须通过execute执行. C:函数可以嵌入到SQL语句中执行.而过程不行. 其实我 ...
- Http与Https协议规范
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...
- Java虚拟机05.2(内存分配)
jdk1.7中堆内存分为:年轻代+老年代+永久代.但是永久代有作为非堆内存存在,也就是说堆内存的大小应该为年轻代+老年代.在tomcat容器中,如果jsp页面过多可能出现永久代溢出.通常栈溢出都是程序 ...
- 005.Oracle数据库 , 查询多字段连接合并,并添加文本内容
/*Oracle数据库查询日期在两者之间*/ SELECT PKID , OCCUR_DATE, PKID || ' 曾经沧海难为水 ' ||TO_CHAR( OCCUR_DATE, ' yyyy/m ...
- 二次urldecode注入
原理大多数web程序都会对输入字符进行转换,例如addslashes(),mysql_real_escape_string(),mysql_escape_string(),也就是对单引号',双引号&q ...
- NOR Flash驱动
驱动程序 1 ] ] );81 ;83 }84 85 86 static void __exit nor_exit(void)87 {88 iounmap(nor_ ...