-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathksubsets.cpp
More file actions
71 lines (67 loc) · 1.71 KB
/
ksubsets.cpp
File metadata and controls
71 lines (67 loc) · 1.71 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using std::cin;
using std::cout;
#include <set>
using std::set;
#include <cstdio>
set<set<int>> ksubsets(set<int> S, size_t k)
{
if (k == 0) { // answer should be {{}}
/* TODO: find a nicer way to do this using
* constructors for set. */
set<int> E; /* it's empty! */
set<set<int>> P;
P.insert(E); /* now P = {{}} */
return P;
}
if (S.size() < k) {
return set<set<int>>(); /* {}, as a set of sets */
}
int x = *(S.begin()); /* let x be an arbitrary element. */
/* NOTE: are we sure S is not empty? Sure we're sure:
* we would have hit one of the base cases otherwise. */
S.erase(x); /* S is now S'... */
set<set<int>> P = ksubsets(S,k);
set<set<int>> R = ksubsets(S,k-1);
/* now add x back to each element of R, and
* union together with P for the final result. */
for (auto i = R.begin(); i != R.end(); i++) {
set<int> T = *i; /* size of T is k-1 */
T.insert(x); /* add x; now |T| = k */
P.insert(T); /* add to union */
}
return P;
}
/* TODO: as with the powerset function from last time, try to write
* this again from scratch, and maybe trace the calls under gdb on
* a small input, or just draw the recursion tree by hand. */
int main(void)
{
int x;
set<int> S;
while (cin >> x) {
S.insert(x);
}
/* compute all subsets of approx. half the size of S: */
set<set<int>> P = ksubsets(S,S.size()/2);
for (auto i = P.begin(); i != P.end(); i++) {
printf("{");
for (auto j = i->begin(); j != i->end(); j++) {
printf("%i ",*j);
}
printf("}\n");
}
return 0;
}
/* TODO: try to write a recursive function that computes all
* n-sequences of a set of characters. E.g., if S={a,b} and
* n = 3, you would have:
* aaa
* aab
* aba
* abb
* baa
* bab
* bba
* bbb
* */