# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
98568 | tdwn | parentrises (BOI18_parentrises) | C++17 | 3 ms | 384 KiB |
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>
#define ll long long
using namespace std;
string code;
string answ;
string fin;
string solve(string code) {
string answ = code;
stack<int>s1, s2;
for(int i=0;i<code.length();i++) {
if(code[i] == '(') {
s1.push(i);
}
else {
if(s1.size() > 0) {
answ[s1.top()] = 'B';
answ[i] = 'B';
s2.push(s1.top());
s1.pop();
}
else if(s2.size() > 0){
answ[s2.top()] = 'G';
answ[i] = 'R';
s2.pop();
}
}
}
return answ;
}
bool invalid(char a, char b) {
return (a == '(' && b == a) || (a == ')' && b == a) || (a == 'R' && b == 'B') || (a == 'B' && b == 'R');
}
void solvea() {
cin>>code;
string f = solve(code);
reverse(code.begin(), code.end());
for(int i=0;i<code.length();i++) {
if(code[i] == '(') code[i] = ')';
else code[i] = '(';
}
string s = solve(code);
reverse(s.begin(), s.end());
// merge them
string answ = f;
for(int i=0;i<f.length();i++) {
if(invalid(s[i], f[i])) {
cout<<"Impossible\n";
return;
}
else {
if(s[i] == 'G' || f[i] == 'G') answ[i] = 'G';
else if(s[i] == f[i]) answ[i] = s[i];
else if((s[i] != '(' && s[i] != ')') && (f[i] =='(' || f[i] == ')')) answ[i] = s[i];
else if((f[i] != '(' && f[i] != ')') && (s[i] =='(' || s[i] == ')')) answ[i] = f[i];
}
if(answ[i] == '(' || answ[i] == ')') {
cout<<"Impossible\n";
return;
}
}
cout<<answ<<"\n";
}
void solveb() {
cout<<"0\n";
}
int main() {
int P, T;
cin>>P;
cin>>T;
while(T--) {
if(P == 1) solvea();
else solveb();
}
return 0;
}
Compilation message (stderr)
# | 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... |