/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int p;
cin >> p;
int t;
cin >> t;
while(t--){
string s;
cin >> s;
int n = s.size();
char ans[n];
int n1 = 0;
int n2 = 0;
for(int i = 0; i < n; i++){
ans[i] = '?';
if(s[i] == '('){
n1 ++;
}else{
n2 ++;
}
}
int t2 = 0;
for (int x = 0; x <= n1; x++){
int y = x - (n2 - n1);
int x1 = x;
int y1 = y;
if(y1 >= 0 and t2 == 0){
int t1 = 0;
for(int i = 0; i < n; i++){
if(s[i] == '(' and x1 > 0){
ans[i] = 'G';
x1 --;
}
}
for(int i = n - 1; i >= 0; i--){
if(s[i] == ')' and y1 > 0){
ans[i] = 'G';
y1 --;
}
}
stack<int>s1;
int trenlijevi = 0;
int trendesni = 0;
stack<int>s2;
for(int i = 0; i < n; i++){
if(ans[i] == 'G'){
if(s[i] == '('){
s1.push(i);
s2.push(i);
}else{
if(s1.empty() or s2.empty()){
t1++;
}else{
s1.pop();
s2.pop();
}
}
}else{
if(s[i] == '('){
if(trenlijevi == 0){
s1.push(i);
ans[i] = 'B';
trenlijevi ++;
}else{
s2.push(i);
ans[i] = 'R';
trenlijevi = 0;
}
}else{
if(trendesni == 0){
if(s1.empty()){
t1++;
}else{
s1.pop();
ans[i] = 'B';
}
trendesni = 1;
}else{
if(s2.empty()){
t1++;
}else{
s2.pop();
ans[i] = 'R';
}
trendesni = 0;
}
}
}
}
if(!s1.empty() or !s2.empty()){
t1++;
}
if(t1 == 0){
t2++;
}
}
}
if(t2 == 0){
cout<<"impossible"<<endl;
}else{
for(int i = 0; i < n; i++){
cout<<ans[i];
}
cout<<endl;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 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 |
344 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 |
344 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 |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 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 |
344 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 |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
348 KB |
Output is correct |
13 |
Correct |
12 ms |
344 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
11 ms |
348 KB |
Output is correct |
16 |
Correct |
20 ms |
468 KB |
Output is correct |
17 |
Correct |
93 ms |
832 KB |
Output is correct |
18 |
Execution timed out |
1018 ms |
768 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Expected integer, but "impossible" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Expected integer, but "impossible" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
600 KB |
Expected integer, but "impossible" found |
2 |
Halted |
0 ms |
0 KB |
- |