Ch8.3: find all the subsets of a set
2014-01-30 04:01
363 查看
In this problem, We can still use the power of BINARY number to represent the nth number is shown or not.
For example:
{2, 0, 1, 4}, if I use _ _ _ _ (4 bits) to represent each numbers. Then I can have 2^4 combinations. eg.: 1011 means {2, 1, 4}, 0010 means {1}, 0000 means {empty}.
Another method is recursion. Because this problem can be divide into small problems and solve it.
Whole code:
For example:
{2, 0, 1, 4}, if I use _ _ _ _ (4 bits) to represent each numbers. Then I can have 2^4 combinations. eg.: 1011 means {2, 1, 4}, 0010 means {1}, 0000 means {empty}.
Another method is recursion. Because this problem can be divide into small problems and solve it.
Whole code:
// hawstein ch 8-3: subsets of a set #include #include using namespace std; // method 1: {1,2,3,4} have 4 elements so have 2^n-1=2^4-1=15 subsets // according: they can be represented by shown/not shown, or say by 0000~1111 // TIPS: 2^n can be represented by 1<> vvi; typedef vector vi; vvi get_subsets(int a[], int n){ vvi big_subsets; int m = 1<0){ if(j&1) small_subsets.push_back(a[idx]); j>>=1; ++idx; } big_subsets.push_back(small_subsets); } return big_subsets; } // method 2: recursion. This problem can be deductive from hard problem to small // ones: for example: // {1,2} has subsets: {},{1},{2},{1,2} // {1,2,3} has subsets: {},{1},{2},{1,2},{3},{1,3},{2,3},{1,2,3} // subsets(1,2,3)-subsets(1,2) = {3},{1,3},{2,3},{1,2,3} // {3},{1,3},{2,3},{1,2,3} = {},{1},{2},{1,2} and add {3} in them. vvi get_subsets1(int a[], int n, int idx){ vvi big_subsets; if(idx==n) { vi small_subsets; big_subsets.push_back(small_subsets); // empty set } else{ vvi tmp_big_subsets = get_subsets1(a, n, idx+1); int tmp = a[idx]; for(int i=0; i
相关文章推荐
- Given an array of size n, find all the possible sub set of the array of size k
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- 1. MaxCounters 计数器 Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum.
- LeetCode Online Judge:Given a set of distinct integers, S, return all possible subsets.
- Could not find version 8.3 of the MCR. Attempting to load mclmcrrt8_3.dll
- hdu 1231, dp ,maximum consecutive sum of integers, find the boundaries, possibly all negative, C++
- Find the all applications of ubuntu
- all objects of the same class share the same set of class methods
- Problem 10 of Find the sum of all the primes below two million.
- Find all the permutations of a string
- Problem 1:Find the sum of all the multiples of 3 or 5 below 1000.
- 数组空间Given a sequence of numbers (or array).Find the maximum distance between all the same numbers.
- Algorithm_Write a method that returns all subsets of a set.
- find all the permutation of a string
- Q9.4 Write a method that returns all subsets of a set.
- Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
- To find the list of all the available drives on...
- Spatial index is slow when trying to find all the points within a range of a geocode.
- CareerCup Find all the conflicting appointments from a given list of n appointments.
- 输出一个集合所有子集的元素和(Print sums of all subsets of a given set)