blob: d22041dce910f6cc048027aaaf2e058287603b8b (
plain)
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
|
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
int main() {
std::array<size_t, 26> a{};
std::string s;
std::cin >> s;
for (auto c : s) a[c-'A']++;
size_t i{0}, j{1}, tot{s.size()}, c{99};
std::stringstream ss{};
while (*std::max_element(a.begin(), a.end()) <= tot / 2) {
while (a[i] == 0) i++;
while (a[j] == 0 || j <= i) j++;
c = i == c ? j : i;
ss << (char)('A' + c);
a[c]--;
tot--;
}
j = std::distance(a.begin(), std::max_element(a.begin(), a.end()));
while (a[j] > 1) {
if (tot == a[j]) {
std::cout << "-1\n";
return 0;
}
while (a[i] == 0 || i == j) i++;
ss << (char)('A' + j) << (char)('A' + i);
a[i]--;
a[j]--;
tot -= 2;
}
std::cout << ss.str() << (char)('A' + j) << "\n";
}
|