-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1622 - Creating Strings.cpp
More file actions
56 lines (48 loc) · 1 KB
/
1622 - Creating Strings.cpp
File metadata and controls
56 lines (48 loc) · 1 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
#include <bits/stdc++.h>
using namespace std;
#define input "input.in"
#define output "output.out"
void dfs(string s, map<char, int> m) {
if (m.empty()) {
cout << s << "\n";
return;
}
map<char, int> m1(m);
for (auto [k, v] : m1) {
if (v == 1)
m.erase(k);
else
m[k]--;
dfs(s + k, m);
m[k]++;
}
}
int cnk(int n, int k) {
int ans = 1, n_k_fact = 1;
for (auto i = n; i > k; i--) ans *= i;
for (auto i = n - k; i > 0; i--) n_k_fact *= i;
return ans / n_k_fact;
}
void solve() {
string s;
cin >> s;
map<char, int> m;
for (auto c : s) m[c]++;
int k = 1, rest = s.length();
for (auto [_, v] : m) {
k *= cnk(rest, v);
rest -= v;
}
cout << k << "\n";
dfs("", m);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(input, "r")) freopen(input, "r", stdin);
if (fopen(output, "r")) freopen(output, "w+", stdout);
int t = 1;
// cin >> t;
while (t--) solve();
}