codechef MARCHA1 - "paying up": http://www.codechef.com/problems/MARCHA1
tutorial: http://www.codechef.com/wiki/tutorial-paying
subset sum problem explained in dp(dynamic programming): http://www.geeksforgeeks.org/dynamic-programming-subset-sum-problem/
rutgers university algo for subset sum problem: http://crab.rutgers.edu/~guyk/ex/part.pdf
reduction of subset-sum-optimization to subset-sum-decision(washington university): http://courses.cs.washington.edu/courses/csep521/05sp/lectures/sss.pdf
carnegie-mellon courtesy, subset-sum and knapsack explained: http://www.cs.cmu.edu/~ckingsf/bioinfo-lectures/subsetsum.pdf
i suggest to have a look at MIT knapsack recitation: http://www.youtube.com/watch?v=wFP5VHGHFdk
subset sum(backtracking) video: http://www.youtube.com/watch?v=WRT8kmFOQTw
my c++ solution to codechef MARCHA1 - "paying up" problem: http://ideone.com/p04R4k
// A recursive solution for subset sum problem
#include <stdio.h>
#include <iostream>
using namespace std;
// Returns true if there is a subset of set[] with sun equal to given sum
bool isSubsetSum(int set[], int n, int sum)
{
//cout<<set[n]<<" "<<n<<" "<<sum<<endl; i've printed it to learn what my code is doing
// Base Cases
if (sum == 0)
return true;
if (n == 0 && sum != 0)
return false;
// If last element is greater than sum, then ignore it
if (set[n-1] > sum)
return isSubsetSum(set, n-1, sum);
/* else, check if sum can be obtained by any of the following
(a) including the last element
(b) excluding the last element */
return isSubsetSum(set, n-1, sum) || isSubsetSum(set, n-1, sum-set[n-1]);
}
// Driver program to test above function
int main()
{
int set[100];
int sum;
int a, b;
//int n = sizeof(set)/sizeof(set[0]);
cin>>a;
for(int z=0; z<a; z++) {
int n;
cin>>n>>sum;
for(int k=0; k<n; k++) cin>>set[k];
if (isSubsetSum(set, n, sum) == true)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
NOTE: to be honest, i myself don't have any idea about what my code is doing. i havent understood the topic yet, so i appreciate any guiding comments. thnx
No comments:
Post a Comment