-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1342B.cpp
65 lines (57 loc) · 1.33 KB
/
1342B.cpp
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
#include <bits/stdc++.h>
#define FOR(i, start, end) for(int i = start; i < end; i++)
#define ROF(i, start, end) for(int i = end; i > start; i--)
#define FORO(i, end) for(int i = 0; i < end; i++)
#define ROFO(i, start) for(int i = start; i >= 0; i--)
#define pub push_back
#define fir first
#define sec second
#define pob pop_back
#define read_arr(arr, start, end) for (int i = start; i < end; i++) { cin >> arr[i]; }
#define vi vector<int>
#define vl vector<long>
#define pii pair<int, int>
using namespace std;
typedef long long LL;
typedef long double LD;
bool cmp(pair<int, string> a, pair<int, string> b) {
return a.first < b.first;
}
void solve() {
string t;
cin >> t;
// ZERO, ONE index
vi z, o;
vector<pair<int, string>> in;
int added = 0;
FORO(i, t.length()) {
if (t[i] == '0') z.pub(i);
else o.pub(i);
}
if (z.size() && o.size()) {
FORO(i, o.size()-1) {
if (abs(o[i] - o[i+1]) == 1) {
// add additional 0
in.pub({o[i+1], "0"});
}
}
FORO(i, z.size()-1) {
if (abs(z[i] - z[i+1]) == 1) {
// add additional 1
in.pub({z[i+1], "1"});
}
}
sort(in.begin(), in.end(), cmp);
ROFO(i, in.size()-1) {
t.insert(in[i].first, in[i].second);
}
}
cout << t << '\n';
}
int main() {
int n;
cin >> n;
while (n--) {
solve();
}
}