#include<bits/stdc++.h>
using namespace std;
int N;
char sir[1000009], ans[1000009];
const int mod = 1e9 + 7;
const int maxN = 300;
int cnt[maxN + 5], dp[maxN + 5][maxN + 5][2][4];
void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}
void addGreens (int l, int r)
{
while (1)
{
while (sir[l] == ')' && l <= N) l ++;
while (sir[r] == '(' && r >= 1) r --;
if (l == N + 1 || r == 0) break;
if (l < r) ans[l] = ans[r] = 'G', l ++, r --;
else break;
}
}
void fillOutRB (bool &ok)
{
int j = 0, k = 0;
for (int i=1; i<=N; i++)
{
assert (j <= k);
if (ans[i] == 'G')
{
if (sir[i] == '(') j ++, k ++;
else j --, k --;
if (j < 0 || k < 0) ok = 0;
continue;
}
if (sir[i] == '(')
{
if (j >= k) k ++, ans[i] = 'B';
else j ++, ans[i] = 'R';
continue;
}
if (j < k) k --, ans[i] = 'B';
else j --, ans[i] = 'R';
if (j < 0) ok = 0;
}
if (!ok) assert (j + k == 0);
}
namespace generator {
const int N = 3;
bool canDo = 0;
void back (int pos, int i, int j)
{
if (canDo) return ;
if (i < 0 || j < 0) return ;
if (pos == N + 1)
{
if (i + j == 0)
canDo = 1;
return ;
}
back (pos + 1, i + sir[pos], j + sir[pos]);
back (pos + 1, i + sir[pos], j);
back (pos + 1, i, j + sir[pos]);
}
void genTests ()
{
vector < string > input;
for (int msk = 0; msk < (1 << N); msk ++)
{
for (int i=0; i<N; i++)
sir[i + 1] = (((msk & (1 << i)) > 0) ? +1 : -1);
canDo = 0, back (1, 0, 0);
if (canDo)
{
string curr;
for (int i=1; i<=N; i++)
if (sir[i] == +1) curr.push_back ('(');
else curr.push_back (')');
input.push_back (curr);
}
}
printf ("%d\n", input.size ());
for (auto s : input)
printf ("%s\n", s.c_str ());
exit (0);
}
}
int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);
//generator::genTests ();
int type;
scanf ("%d", &type);
if (type == 1)
{
int tests;
scanf ("%d\n", &tests);
while (tests --)
{
scanf ("%s\n", sir + 1), N = strlen (sir + 1);
int balance = 0;
for (int i=1; i<=N; i++)
if (sir[i] == '(') balance ++;
else balance --;
for (int i=1; i<=N + 1; i++)
ans[i] = 0;
bool ok = 1;
if (balance > 0)
{
///I'll use first X + balance open brackets and last X closed ones where X is maximal so that all the closed brackets come after the open ones
int l = 1, r = N;
while (balance > 0)
{
while (sir[r] == '(' && r >= 1) r --;
if (r == 0)
{
ok = 0;
break;
}
ans[r] = 'G', r --;
balance --;
}
if (ok)
addGreens (l, r);
}
else
{
balance = -balance;
///same as above
int l = 1, r = N;
while (balance > 0)
{
while (sir[l] == ')' && l <= N) l ++;
if (l == N + 1)
{
ok = 0;
break;
}
ans[l] = 'G', l ++;
balance --;
}
if (ok)
addGreens (l, r);
}
if (ok)
fillOutRB (ok);
if (!ok) printf ("impossible\n");
else
{
for (int i=1; i<=N; i++)
printf ("%c", ans[i]);
printf ("\n");
}
}
return 0;
}
///a state is defined by:
///j, kj (which means (j, k) = (j, j + kj))
///the current phase: 0/1/2/3
///in phase 0 we need to take any ( and put it as green
///in phases 1 and 2 we can't put any green and we can't have the subsequence () -
///phase 1 is as long as we have )
///phase 2 is as long as we have ( after phase 1 (and after that both phases must stop since we can't have () )
///in phase 3 we need to take any ) and put it as green
///complexity: N^2
///dp[i][j][kj][phase]
dp[0][0][0][0] = 1;
for (int i=0; i<maxN; i++)
{
for (int j=0; j<=i; j++)
for (int kj=0; kj<2; kj++)
for (int phase=0; phase<4; phase++)
if (dp[i][j][kj][phase] > 0)
for (int br = 0; br < 2; br ++)
for (int nPhase = phase; nPhase < 4; nPhase ++)
{
int ni = i + 1, nj = j, nk = j + kj;
if (phase == 1 && br == 0 && nPhase == 1) continue;
if (phase == 2 && br == 1 && nPhase == 2) continue;
if (phase == 1 && nPhase == 2 && br != 0) continue;
if (phase == 0 && br == 0 && nPhase == 1) continue;
if (phase == 0 && 1 <= nPhase && nPhase <= 2 && br == 1) continue;
if (nPhase == 3 && 0 <= phase && phase <= 2 && br == 0) continue;
if ((nPhase == 3 && br == 1) || (nPhase == 0 && br == 0))
{
if (br == 0) nj ++, nk ++;
else nj --, nk --;
}
else
{
if (br == 0)
{
if (nj >= nk) nk ++;
else nj ++;
}
else
{
if (nj < nk) nk --;
else nj --;
}
}
if (nj >= 0 && nk >= 0)
adto (dp[ni][nj][nk - nj][nPhase], dp[i][j][kj][phase]);
}
for (int phase = 0; phase < 4; phase ++)
adto (cnt[i + 1], dp[i + 1][0][0][phase]);
}
int tests;
scanf ("%d", &tests);
while (tests --)
{
scanf ("%d", &N);
printf ("%d\n", cnt[N]);
}
return 0;
}
Compilation message
parentrises.cpp: In function 'void generator::genTests()':
parentrises.cpp:86:38: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<std::__cxx11::basic_string<char> >::size_type {aka long unsigned int}' [-Wformat=]
printf ("%d\n", input.size ());
~~~~~~~~~~~~~^
parentrises.cpp: In function 'int main()':
parentrises.cpp:101:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d", &type);
~~~~~~^~~~~~~~~~~~~
parentrises.cpp:105:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d\n", &tests);
~~~~~~^~~~~~~~~~~~~~~~
parentrises.cpp:108:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%s\n", sir + 1), N = strlen (sir + 1);
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
parentrises.cpp:219:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d", &tests);
~~~~~~^~~~~~~~~~~~~~
parentrises.cpp:222:11: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf ("%d", &N);
~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
436 KB |
Output is correct |
2 |
Correct |
3 ms |
436 KB |
Output is correct |
3 |
Correct |
3 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
556 KB |
Output is correct |
2 |
Correct |
3 ms |
556 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
556 KB |
Output is correct |
2 |
Correct |
3 ms |
556 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
556 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
4 ms |
620 KB |
Output is correct |
8 |
Correct |
4 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
556 KB |
Output is correct |
2 |
Correct |
3 ms |
556 KB |
Output is correct |
3 |
Correct |
2 ms |
556 KB |
Output is correct |
4 |
Correct |
3 ms |
556 KB |
Output is correct |
5 |
Correct |
2 ms |
556 KB |
Output is correct |
6 |
Correct |
2 ms |
620 KB |
Output is correct |
7 |
Correct |
4 ms |
620 KB |
Output is correct |
8 |
Correct |
4 ms |
620 KB |
Output is correct |
9 |
Correct |
3 ms |
620 KB |
Output is correct |
10 |
Correct |
3 ms |
620 KB |
Output is correct |
11 |
Correct |
4 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
620 KB |
Output is correct |
13 |
Correct |
3 ms |
636 KB |
Output is correct |
14 |
Correct |
4 ms |
640 KB |
Output is correct |
15 |
Correct |
3 ms |
640 KB |
Output is correct |
16 |
Correct |
8 ms |
764 KB |
Output is correct |
17 |
Correct |
12 ms |
1048 KB |
Output is correct |
18 |
Correct |
11 ms |
1048 KB |
Output is correct |
19 |
Correct |
10 ms |
1048 KB |
Output is correct |
20 |
Correct |
15 ms |
1048 KB |
Output is correct |
21 |
Correct |
52 ms |
1332 KB |
Output is correct |
22 |
Correct |
96 ms |
3500 KB |
Output is correct |
23 |
Correct |
81 ms |
3500 KB |
Output is correct |
24 |
Correct |
72 ms |
3500 KB |
Output is correct |
25 |
Correct |
88 ms |
3528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3528 KB |
Output is correct |
2 |
Correct |
16 ms |
3528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3528 KB |
Output is correct |
2 |
Correct |
16 ms |
3528 KB |
Output is correct |
3 |
Correct |
17 ms |
3528 KB |
Output is correct |