#include <bits/stdc++.h>
using namespace std;
string solve(const string &test) {
const int N = test.size();
string redMask(N, ' ');
string blueMask(N, ' ');
vector <int> redStk;
queue <int> redOpened;
for (int i = 0; i < N; ++ i) {
const char ch = test[i];
if (ch == '(') {
redMask[i] = 'R';
redStk.push_back(i);
redOpened.push(i);
}
else if (ch == ')') {
if (!redStk.empty()) {
redMask[i] = 'R';
redStk.pop_back();
}
else if (redOpened.empty())
return "impossible";
else {
blueMask[i] = 'B';
blueMask[redOpened.front()] = 'B';
redOpened.pop();
}
}
}
reverse(redStk.begin(), redStk.end());
stack <int> redClosed;
int i = N - 1;
for (const int pos: redStk) {
while (i > pos) {
if (test[i] == ')')
redClosed.push(i);
-- i;
}
if (redClosed.empty())
return "impossible";
else {
redMask[pos] = ' ';
blueMask[pos] = 'B';
const int transformToGreen = redClosed.top();
redClosed.pop();
blueMask[transformToGreen] = 'B';
}
}
string coloring;
for (i = 0; i < N; ++ i)
if (redMask[i] == 'R' && blueMask[i] == 'B')
coloring += 'G';
else if (redMask[i] == 'R')
coloring += 'R';
else if (blueMask[i] == 'B')
coloring += 'B';
else
assert(0);
return coloring;
}
int main() {
int type; cin >> type;
if (type == 2) {
return 0;
}
int T;
cin >> T;
while (T --) {
// Read test
string str;
cin >> str;
// Check validity
assert(1 <= str.size() && str.size() <= 1000000);
for (auto it: str)
assert(it == '(' || it == ')');
cout << solve(str) << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
488 KB |
Output is correct |
3 |
Correct |
3 ms |
520 KB |
Output is correct |
4 |
Correct |
2 ms |
520 KB |
Output is correct |
5 |
Correct |
2 ms |
520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
540 KB |
Output is correct |
2 |
Correct |
3 ms |
540 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
540 KB |
Output is correct |
2 |
Correct |
3 ms |
540 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
540 KB |
Output is correct |
2 |
Correct |
3 ms |
540 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
620 KB |
Output is correct |
8 |
Correct |
3 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
11 |
Correct |
6 ms |
620 KB |
Output is correct |
12 |
Correct |
4 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
636 KB |
Output is correct |
14 |
Correct |
4 ms |
748 KB |
Output is correct |
15 |
Correct |
3 ms |
748 KB |
Output is correct |
16 |
Correct |
35 ms |
748 KB |
Output is correct |
17 |
Correct |
16 ms |
1452 KB |
Output is correct |
18 |
Correct |
12 ms |
1452 KB |
Output is correct |
19 |
Correct |
9 ms |
1452 KB |
Output is correct |
20 |
Correct |
8 ms |
1580 KB |
Output is correct |
21 |
Correct |
224 ms |
1580 KB |
Output is correct |
22 |
Correct |
87 ms |
8732 KB |
Output is correct |
23 |
Correct |
60 ms |
8732 KB |
Output is correct |
24 |
Correct |
61 ms |
8732 KB |
Output is correct |
25 |
Correct |
65 ms |
8732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8732 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8732 KB |
Unexpected end of file - int32 expected |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8732 KB |
Unexpected end of file - int32 expected |