#include "bits/stdc++.h"
using namespace std;
#define int long long
pair <int,int> mn(pair <int,int> a, pair <int,int> b) {
if(max(a.first, a.second) < max(b.first, b.second) ) return a;
return b;
}
signed main () {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
int q;
cin >> q;
while(q --) {
string str;
cin >> str;
int n = str.size();
stack <int> st;
vector <int> cnt(n, 0), nopeopen, nopeclose;
for(int i = 0; i < n; i ++) {
if(str[i] == ')') {
if(st.size() == 0) {
nopeclose.push_back(i);
continue;
}
cnt[i] ++;
st.pop();
}else {
st.push(i);
cnt[i] ++;
}
}
while(st.size() != 0) nopeopen.push_back(st.top()), st.pop();
set <int> close, open;
for(int i = 0; i < str.size(); i ++) {
if(cnt[i] == 1) {
if(str[i] == '(') open.insert(i);
else close.insert(i);
}
}
sort(nopeopen.rbegin(), nopeopen.rend());
for(auto i : nopeopen) {
if(close.size() == 0) {
cout << "impossible\n";
goto end;
}
if(i > *--close.end()) {
cout << "impossible\n";
goto end;
}
cnt[*--close.end()] ++;
close.erase(--close.end());
cnt[i] = 5;
}
sort(nopeclose.begin(), nopeclose.end());
for(auto i : nopeclose) {
if(open.size() == 0) {
cout << "impossible\n";
goto end;
}
if(i < *open.begin()) {
cout << "impossible\n";
goto end;
}
cnt[*open.begin()] ++;
open.erase(open.begin());
cnt[i] = 5;
}
for(auto i : cnt) {
if(i == 2) cout << "G";
else if(i == 1) cout << "R";
else cout << "B";
assert(i > 0);
}
cout << "\n";
continue;
end:;
}
return 0;
}
Compilation message
parentrises.cpp: In function 'int main()':
parentrises.cpp:40:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < str.size(); i ++) {
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
504 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
504 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
860 KB |
Output is correct |
16 |
Correct |
7 ms |
484 KB |
Output is correct |
17 |
Correct |
15 ms |
5976 KB |
Output is correct |
18 |
Correct |
12 ms |
1116 KB |
Output is correct |
19 |
Correct |
13 ms |
3164 KB |
Output is correct |
20 |
Correct |
15 ms |
5980 KB |
Output is correct |
21 |
Correct |
66 ms |
1324 KB |
Output is correct |
22 |
Correct |
184 ms |
57068 KB |
Output is correct |
23 |
Correct |
126 ms |
7616 KB |
Output is correct |
24 |
Correct |
146 ms |
29496 KB |
Output is correct |
25 |
Correct |
190 ms |
58092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Expected integer, but "B" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Expected integer, but "B" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Expected integer, but "B" found |
2 |
Halted |
0 ms |
0 KB |
- |