#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int mod = 1e9 + 7;
int P;
int readable(string &s){
int n = s.size();
map<pair<int, int>, bool> dp[n];
if(s[0] == ')' or s[n-1] == '('){
return 0;
}
dp[0][{0, 1}] = 1, dp[0][{1, 0}] = 1;
dp[0][{1, 1}] = 1;
for(int i = 1;i < n; i++){
for(auto [pp, y] : dp[i-1]){
int x1 = pp.ff, x2 = pp.ss;
int ok = 0;
int c = s[i]==')' ? -1 : 1;
if(x2 + c >= 0){
ok++;
dp[i][{x1, x2 + c}] = 1;
}
if(x1 + c >= 0){
ok++;
dp[i][{x1 + c, x2}] = 1;
}
if(ok == 2){
dp[i][{x1 + c, x2 + c}] = 1;
}
}
}
if(dp[n-1][{0, 0}]){
return 1;
}else{
return 0;
}
}
void solve(){
string s; cin >> s;
int n = s.size();
map<pair<int, int>, bool> dp[n];
string path[n][n + 1][n + 1];
// RBG
if(s[0] == ')' or s[n-1] == '('){
cout << "impossible";
return;
}
dp[0][{0, 1}] = 1, dp[0][{1, 0}] = 1;
dp[0][{1, 1}] = 1;
path[0][0][1] = "R";
path[0][1][1] = "G";
path[0][1][0] = "B";
vector<int> pref(n);
pref[0] = 1;
for(int i = 1;i < n; i++){
pref[i] = pref[i-1] + s[i]==')' ? -1 : 1;
}
for(int i = 1;i < n; i++){
for(auto [pp, y] : dp[i-1]){
int x1 = pp.ff, x2 = pp.ss;
int ok = 0;
int c = s[i]==')' ? -1 : 1;
if(x2 + c >= 0){
ok++;
dp[i][{x1, x2 + c}] = 1;
path[i][x1][x2 + c] = path[i-1][x1][x2] + "R";
}
if(x1 + c >= 0){
ok++;
dp[i][{x1 + c, x2}] = 1;
path[i][x1 + c][x2] = path[i-1][x1][x2] + "B";
}
if(ok == 2){
dp[i][{x1 + c, x2 + c}] = 1;
path[i][x1 + c][x2 + c] = path[i-1][x1][x2] + "G";
}
}
}
if(dp[n-1][{0, 0}]){
cout << path[n-1][0][0];
}else{
cout << "impossible";
}
}
void solve1(){
int n; cin >> n;
int ans = 0;
for(int mask = 0; mask < (1<<n); mask++){
string s = "";
for(int i = 0;i < n; i++){
if(mask & (1<<i)) s.push_back('(');
else s.push_back(')');
}
ans+= readable(s);
}
cout << ans;
}
main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> P;
int test; cin >> test;
while(test--){
if(P == 1)solve();
else solve1();
cout << '\n';
}
return 0;
}
Compilation message
parentrises.cpp:109:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
109 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16732 KB |
Output is correct |
2 |
Correct |
53 ms |
46672 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
6 ms |
5468 KB |
Output is correct |
5 |
Correct |
46 ms |
42836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16732 KB |
Output is correct |
2 |
Correct |
53 ms |
46672 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
6 ms |
5468 KB |
Output is correct |
5 |
Correct |
46 ms |
42836 KB |
Output is correct |
6 |
Runtime error |
130 ms |
262144 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
16732 KB |
Output is correct |
2 |
Correct |
53 ms |
46672 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
6 ms |
5468 KB |
Output is correct |
5 |
Correct |
46 ms |
42836 KB |
Output is correct |
6 |
Runtime error |
130 ms |
262144 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
636 KB |
Output is correct |
2 |
Execution timed out |
1064 ms |
596 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
636 KB |
Output is correct |
2 |
Execution timed out |
1064 ms |
596 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |