This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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++) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |