-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations_klex.cpp
More file actions
148 lines (134 loc) · 3.92 KB
/
combinations_klex.cpp
File metadata and controls
148 lines (134 loc) · 3.92 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <string>
#include <vector>
#include <iostream>
template<typename Item_type>
void combinations_klex1(
std::vector<Item_type> &&prefix,
std::vector<Item_type> &&items,
std::size_t combo_size,
std::vector<std::vector<Item_type>> &out
)
{
if (items.size() < combo_size) { return; }
else if (combo_size == 0) {
out.push_back(prefix);
} else {
auto t = prefix;
t.push_back(items[0]);
combinations_klex1(std::move(t), {items.begin() + 1, items.end()}, combo_size - 1, out);
combinations_klex1(std::move(prefix), {items.begin() + 1, items.end()}, combo_size, out);
}
}
template<typename Item_type>
std::vector<std::vector<Item_type>> combinations_klex1(std::vector<Item_type> items, std::size_t combo_size)
{
std::vector<std::vector<Item_type>> out;
combinations_klex1({}, std::move(items), combo_size, out);
return out;
}
void combinations_klex1(
std::string &&prefix,
std::string &&in_string,
std::size_t combo_size,
std::vector<std::string> &out
)
{
if (in_string.size() < combo_size) { return; }
else if (combo_size == 0) {
out.push_back(prefix);
} else {
combinations_klex1(prefix + in_string[0], in_string.substr(1, std::string::npos), combo_size - 1, out);
combinations_klex1(std::move(prefix), in_string.substr(1, std::string::npos), combo_size, out);
}
}
std::vector<std::string> combinations_klex1(std::string in_string, std::size_t combo_size)
{
std::vector<std::string> out;
combinations_klex1("", std::move(in_string), combo_size, out);
return out;
}
template<typename Item_type>
void combinations_klex2(
std::vector<Item_type> &&prefix,
std::vector<Item_type> &&items,
std::size_t combo_size,
std::vector<std::vector<Item_type>> &out
)
{
if (combo_size == 0) {
out.push_back(prefix);
} else {
for (auto i = 0; i < items.size(); ++i) {
auto t = prefix;
t.push_back(items[i]);
combinations_klex2(
std::move(t),
{items.begin() + i + 1, items.end()},
combo_size - 1,
out
);
}
}
}
template<typename Item_type>
std::vector<std::vector<Item_type>> combinations_klex2(std::vector<Item_type> items, std::size_t combo_size)
{
std::vector<std::vector<Item_type>> out;
combinations_klex1({}, std::move(items), combo_size, out);
return out;
}
void combinations_klex2(
std::string &&prefix,
std::string &&in_string,
std::size_t combo_size,
std::vector<std::string> &out
)
{
if (combo_size == 0) {
out.push_back(prefix);
} else {
for (std::size_t i{0}; i < in_string.size(); ++i) {
combinations_klex2(
prefix + in_string[i],
in_string.substr(i + 1, std::string::npos),
combo_size - 1,
out
);
}
}
}
std::vector<std::string> combinations_klex2(std::string in_string, std::size_t combo_size)
{
std::vector<std::string> out;
combinations_klex2("", std::move(in_string), combo_size, out);
return out;
}
int main()
{
auto out = combinations_klex1("abcde", 3);
for (const auto &s : out) {
std::cout << s << "\n";
}
std::cout << "\n";
auto out2 = combinations_klex2("abcde", 3);
for (const auto &s : out2) {
std::cout << s << "\n";
}
auto t = std::vector<int>{1, 2, 3, 4, 5};
auto out3 = combinations_klex1(t, 3);
for (const auto &v : out3) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << "\n";
}
std::cout << "\n";
auto out4 = combinations_klex2(t, 3);
for (const auto &v : out4) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << "\n";
}
return 0;
}