-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsubset_gen.cpp
57 lines (47 loc) · 984 Bytes
/
subset_gen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <vector>
#include <iostream>
using namespace std;
int cnt;
vector<int> subset;
void print_vec(vector<int> vec)
{
for (int i=0; i < (int) vec.size(); ++i) {printf("%d ", vec[i]);}
}
void search(int k, int n)
{
if (k == n)
{
//process subset
printf("%d: ", cnt++); print_vec(subset); printf("\n");
}
else
{
search(k+1, n);
subset.push_back(k);
search(k+1, n);
subset.pop_back();
}
}
void bitseq(int n)
{
for (int b = 0; b < (1 << n); ++b)
{
vector<int> subset;
for (int i = 0; i < n; ++i)
{
if (b & (1<<i)){subset.push_back(i);}
}
//process subset
printf("%d: ", cnt++); print_vec(subset); printf("\n");
}
}
int main()
{
int n = 4; //gen all subsets of [0..n]
printf("all subsets of [0..%d]:\n", n);
cnt = 0;
search(0, n); //recursive
//cnt = 0;
//bitseq(n); //iterative
return 0;
}