-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailq_subsets.cpp
More file actions
81 lines (76 loc) · 1.54 KB
/
Copy pathtailq_subsets.cpp
File metadata and controls
81 lines (76 loc) · 1.54 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
72
73
74
75
76
77
78
79
80
81
#include <string>
#include <vector>
#include <stdio.h>
#include <zf_queue.h>
namespace
{
struct item
{
char v;
zf_tailq_node node;
};
typedef zf_tailq_head_t(item, node) items_t;
std::string to_string(items_t &items)
{
std::string s;
for (item *i = zf_tailq_begin_(&items), *e = zf_tailq_end_(&items);
e != i; i = zf_tailq_next_(&items, i))
{
s.push_back(i->v);
}
return s;
}
void subsets_iter(items_t &taken, items_t &items)
{
if (zf_tailq_empty(&items))
{
printf("%s\n", to_string(taken).c_str());
return;
}
item *const i = zf_tailq_first_(&items);
zf_tailq_remove_(&items, i);
// 1. i is out
subsets_iter(taken, items);
// 2. i is in
zf_tailq_insert_head_(&taken, i);
subsets_iter(taken, items);
// 3. cleanup
zf_tailq_remove_(&taken, i);
zf_tailq_insert_head_(&items, i);
}
void subsets(const unsigned n)
{
items_t items = ZF_TAILQ_INITIALIZER(&items);
std::vector<item> storage(n);
for (unsigned i = 0; n > i; ++i)
{
storage[i].v = 'a' + i;
zf_tailq_insert_tail_(&items, &storage[i]);
}
items_t taken = ZF_TAILQ_INITIALIZER(&taken);
subsets_iter(taken, items);
}
}
int main(int argc, char *argv[])
{
try
{
if (1 >= argc)
{
throw std::logic_error("invalid command line arguments");
}
const unsigned n = std::stoi(argv[1]);
if (n > 'z' - 'a')
{
throw std::logic_error("n out of range");
}
subsets(n);
return 0;
}
catch (const std::logic_error &e)
{
fprintf(stderr, "Error: %s\n", e.what());
fprintf(stderr, "Usage: prog SET_SIZE\n");
return 1;
}
}