-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1907B YetnotherrokenKeoard.cpp
77 lines (70 loc) · 1.87 KB
/
1907B YetnotherrokenKeoard.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
66
67
68
69
70
71
72
73
74
75
/*
author : MishkatIT
created : Saturday 2023-12-09-22.27.23
*/
#include<bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define debug(_) cout << #_ << " is " << _ << '\n';
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 10;
const ll inf = 1e9;
const ll linf = 1e18;
int main()
{
fio;
int t;
cin >> t;
while (t--) {
string str;
cin >> str;
stack<pair<char, int>> lower, upper;
for (int i = 0; i < str.size(); i++) {
if (islower(str[i])) {
if (str[i] == 'b') {
if (!lower.empty()) {
lower.pop();
}
} else {
lower.push({str[i], i});
}
} else {
if (str[i] == 'B') {
if (!upper.empty()) {
upper.pop();
}
} else {
upper.push({str[i], i});
}
}
}
vector<pair<char, int>> ans;
while(!lower.empty() && !upper.empty()) {
if (lower.top().second < upper.top().second) {
ans.push_back(lower.top());
lower.pop();
} else {
ans.push_back(upper.top());
upper.pop();
}
}
while(!lower.empty()) {
ans.push_back(lower.top());
lower.pop();
}
while(!upper.empty()) {
ans.push_back(upper.top());
upper.pop();
}
sort(ans.begin(), ans.end(), [&](auto& a, auto& b) {
return (a.second < b.second);
});
for (auto& i : ans) {
cout << i.first;
}
cout << '\n';
}
return 0;
}