#include<bits/stdc++.h>
using namespace std;
int f[2000009];
int dp[2][609][609];
int ans[309];
int mod = 1e9+7;
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;}
}
}
else{
dp[0][0][0] = 1;
for(int i = 0; i <= 300; i++){
int d = i % 2;
int d1 = (i + 1) %2;
for(int l = 0; l <= 600; l++)
for(int r = l; r <= 600; r++)
dp[d1][l][r] = 0;
for(int l = 0; l <= 600; l++)
for(int r = l; r <= 600; r++){
dp[d1][max(0, l-2)][r - 1] += dp[d][l][r];
dp[d1][l + 1][r + 2] += dp[d][l][r];
if(dp[d1][max(0, l - 2)][r - 1] >= mod)
dp[d1][max(0, l - 2)][r - 1] -= mod;
if(dp[d1][l + 1][r + 2] >= mod)
dp[d1][l + 1][r + 2] -= mod;
}
for(int j = 0; j <= 600; j++)
ans[i] = (ans[i] + dp[d][0][j])% mod;
}
int t;
cin >> t;
while(t--){
int N;
cin >> N;
cout << ans[N]<<endl;
}
}
}
Compilation message
parentrises.cpp:7:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
parentrises.cpp: In function 'int main()':
parentrises.cpp:18:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
parentrises.cpp:30:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++)
~~^~~~~~~~~~
parentrises.cpp:36:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
parentrises.cpp:46:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(x < X.size() && y >= 0 && X[x] < Y[y])
~~^~~~~~~~~~
parentrises.cpp:48: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 |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
356 KB |
Output is correct |
3 |
Correct |
2 ms |
432 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
4 ms |
596 KB |
Output is correct |
12 |
Correct |
4 ms |
672 KB |
Output is correct |
13 |
Correct |
3 ms |
672 KB |
Output is correct |
14 |
Correct |
4 ms |
744 KB |
Output is correct |
15 |
Correct |
5 ms |
796 KB |
Output is correct |
16 |
Correct |
29 ms |
796 KB |
Output is correct |
17 |
Correct |
10 ms |
1812 KB |
Output is correct |
18 |
Correct |
15 ms |
1812 KB |
Output is correct |
19 |
Correct |
13 ms |
1812 KB |
Output is correct |
20 |
Correct |
12 ms |
1832 KB |
Output is correct |
21 |
Correct |
225 ms |
1832 KB |
Output is correct |
22 |
Correct |
98 ms |
13328 KB |
Output is correct |
23 |
Correct |
79 ms |
13328 KB |
Output is correct |
24 |
Correct |
73 ms |
13328 KB |
Output is correct |
25 |
Correct |
99 ms |
13332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
13332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
13332 KB |
Output is correct |
2 |
Correct |
203 ms |
13332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
13332 KB |
Output is correct |
2 |
Correct |
203 ms |
13332 KB |
Output is correct |
3 |
Correct |
176 ms |
13332 KB |
Output is correct |