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
|
#define USE_ORDERED_STATISTICS
#define BIG_BIGINT
#include <bits/stdc++.h>
#ifdef USE_ORDERED_STATISTICS
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#endif
using namespace std;
typedef long long int ll;
#ifdef BIG_BIGINT
typedef __int128_t bigint;
#else
typedef ll bigint;
#endif
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpi> vvpi;
typedef vector<vpl> vvpl;
typedef set<int> si;
typedef multiset<int> msi;
typedef set<ll> sl;
typedef multiset<ll> msl;
typedef long double ld;
template<class T> using func = function<T>;
#ifdef USE_ORDERED_STATISTICS
typedef tree<
pl,
null_type,
less<pl>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#endif
#define clrcin cin.ignore(numeric_limits<streamsize>::max(),'\n');
#define GOGOGO ios::sync_with_stdio(false); cin.tie(nullptr);
#define BYEBYE return 0;
#define all(cn) (cn).begin(), (cn).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define csz(c) ((int)c.size())
#define mp make_pair
#define eb emplace_back
#define fi first
#define se second
#define popcnt __builtin_popcount
#define popcntll __builtin_popcount_ll
const int INFI = 1e9 + 5;
const ll INFL = 1e18 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
auto dist = uniform_int_distribution<int>(0, INFI);
auto distll = uniform_int_distribution<ll>(0, INFL);
int rnd() { return dist(rng); }
ll rndl() { return distll(rng); }
#define pb pop_back
#define vvc vector<vector<char>>
#define back push_back
using namespace std;
void print(const vvc& partition) {
for (const auto& subset : partition) {
cout << "{ ";
for (char item : subset) {
cout << item << " ";
}
cout << "} ";
}
cout << endl;
}
void help(vvc& cp,
const string& elements, int n, int k, int idx) {
if (idx == n) {
if (k == 0 || cp.size() == k) {
print(cp);
}
return;
}
for (int i = 0; i < cp.size(); ++i) {
cp[i].back(elements[idx]);
help(cp, elements, n, k, idx + 1);
cp[i].pb();
}
if (k == 0 || cp.size() < k) {
cp.back({elements[idx]});
help(cp, elements, n, k, idx + 1);
cp.pb();
}
}
void gen(const string& elements, int k = 0) {
vvc cp;
help(cp, elements, elements.size(), k, 0);
}
int main() {
string elements = "abc";
gen(elements, 2);
gen(elements);
return 0;
}
|