-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString Reorder.cpp
More file actions
executable file
·67 lines (62 loc) · 1.13 KB
/
Copy pathString Reorder.cpp
File metadata and controls
executable file
·67 lines (62 loc) · 1.13 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
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int t[130];
auto cmp = [](int a, int b) {
if (t[a] == t[b]) return a < b;
return t[a] > t[b];
};
void update(int c, set<int, decltype(cmp)> &q, set<int> &w, char &last) {
q.erase(c);
if (w.find(c) != w.end()) w.erase(c);
t[c]--;
q.insert(c);
if (t[c]) w.insert(c);
last = c;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
for (auto c : s) {
t[int(c)]++;
}
int n = s.size();
int treshold = n / 2;
if (n % 2) treshold++;
set<int, decltype(cmp)> q(cmp);
set<int> w;
for (int i = 'A'; i <= 'Z'; i++) {
if (t[i] > treshold) {
cout << "-1\n";
return 0;
}
q.insert(i);
if (t[i]) w.insert(i);
}
char last = '.';
for (int i = n; i > 0; i--) {
int c = *q.begin();
if (i % 2 && t[c] == treshold) {
cout << char(c);
update(c, q, w, last);
} else {
int c = *w.begin();
if (c != last) {
cout << char(c);
update(c, q, w, last);
} else {
c = *(next(w.begin()));
cout << char(c);
update(c, q, w, last);
}
}
if (i % 2) {
treshold--;
}
}
cout << "\n";
return 0;
}