이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
컴파일 시 표준 에러 (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... |