#include <bits/stdc++.h>
using namespace std;
bool check(string s, string a) {
int b0 = 0, b1 = 0;
for (int i = 0; i < s.size(); i++) {
if (a[i] != 'B')
b0 += (s[i] == '(') ? +1 : -1;
if (a[i] != 'R')
b1 += (s[i] == '(') ? +1 : -1;
if (b0 < 0 || b1 < 0)
return 0;
}
return b0 == 0 && b1 == 0;
}
void solve1() {
string s;
cin >> s;
int n = s.size();
string a(n, 'G');
int b0 = 0, b1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '(') {
a[i] = 'G';
b0++, b1++;
} else {
if (b0 >= b1) {
a[i] = 'R';
b0--;
} else {
a[i] = 'B';
b1--;
}
}
if (b0 < 0 || b1 < 0) {
cout << "impossible\n";
return;
}
}
for (int i = n - 1; i >= 0 && (b0 > 0 || b1 > 0); i--) {
if (s[i] == '(') {
if (b0 >= b1)
a[i] = 'B', b0--;
else
a[i] = 'R', b1--;
} else {
if (b0 > 0 && a[i] == 'B')
a[i] = 'G', b0--;
else if (b1 > 0 && a[i] == 'R')
a[i] = 'G', b1--;
}
assert(b0 >= 0 && b1 >= 0);
}
if (check(s, a))
cout << a << "\n";
else
cout << "impossible\n";
}
void solve2() {}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int p, t;
cin >> p >> t;
while (t--) {
if (p == 1)
solve1();
else
solve2();
}
return 0;
}
Compilation message
parentrises.cpp: In function 'bool check(std::string, std::string)':
parentrises.cpp:6:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
6 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
812 KB |
Output is correct |
18 |
Correct |
2 ms |
424 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
784 KB |
Output is correct |
21 |
Correct |
18 ms |
2100 KB |
Output is correct |
22 |
Correct |
20 ms |
5224 KB |
Output is correct |
23 |
Correct |
12 ms |
2432 KB |
Output is correct |
24 |
Correct |
18 ms |
3096 KB |
Output is correct |
25 |
Correct |
21 ms |
5312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Unexpected end of file - int32 expected |
2 |
Halted |
0 ms |
0 KB |
- |