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;
int f[2000009];
main(){
int P;
cin >> P;
if(P == 1){
int t;
cin >> t;
while(t--){
string s, ans = "";
cin >> s;
int a = 0, b = 0, fl = 0;
for(int i = 0; i < s.size(); i++){
if(s[i] == '(') a++;
else b++;
}
if(a > 2 * b || b > 2 * a){
cout << "impossible"<<endl;
continue;
}
if(a > b){
fl = 1;
swap(a, b);
reverse(s.begin(), s.end());
for(int i = 0; i < s.size(); i++)
if(s[i] == '(') s[i] = ')';
else s[i] = '(';
}
int k = b - a;
vector<int> X, Y;
for(int i = 0; i < s.size(); i++){
f[i] = 0;
if(s[i] == '(') X.push_back(i);
else Y.push_back(i);
}
for(int i = 0; i < k; i++)
f[X[i]] = 1;
int x = k, y = Y.size()-1;
int A = 0, B = 0;
while(x < X.size() && y >= 0 && X[x] < Y[y])
f[X[x]] = 1, f[Y[y]] = 1, x++, y--;
for(int i = 0; i < s.size(); i++){
if(s[i] == '('){
if(f[i]) A++,B++,ans+='G';
else if(A < B) A++,ans+='R';
else B++, ans += 'B';
}
else{
if(f[i]) A--,B--, ans+='G';
else {if(A > B) A--, ans+='R';
else B--,ans+='B';
}
}
if(A < 0 || B < 0) fl = 2;
}
if(fl == 2) cout <<"impossible"<<endl;
else {if (fl) reverse(ans.begin(), ans.end());
cout<<ans<<endl;}
}
}
}
Compilation message (stderr)
parentrises.cpp:4:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
parentrises.cpp: In function 'int main()':
parentrises.cpp:15:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
parentrises.cpp:27:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++)
~~^~~~~~~~~~
parentrises.cpp:33:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
parentrises.cpp:43:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(x < X.size() && y >= 0 && X[x] < Y[y])
~~^~~~~~~~~~
parentrises.cpp:45:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
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... |