using
System;
class
GFG {
static
int
MAX = 300;
static
int
checkFunc(
int
i,
int
j,
string
st)
{
if
(st[i] ==
'('
&& st[j] ==
')'
)
return
1;
if
(st[i] ==
'('
&& st[j] ==
'?'
)
return
1;
if
(st[i] ==
'?'
&& st[j] ==
')'
)
return
1;
if
(st[i] ==
'['
&& st[j] ==
']'
)
return
1;
if
(st[i] ==
'['
&& st[j] ==
'?'
)
return
1;
if
(st[i] ==
'?'
&& st[j] ==
']'
)
return
1;
if
(st[i] ==
'{'
&& st[j] ==
'}'
)
return
1;
if
(st[i] ==
'{'
&& st[j] ==
'?'
)
return
1;
if
(st[i] ==
'?'
&& st[j] ==
'}'
)
return
1;
return
0;
}
static
int
countRec(
int
start,
int
end,
int
[, ] dp,
string
st)
{
int
sum = 0;
if
(start > end)
return
1;
if
(dp[start, end] != -1)
return
dp[start, end];
int
i;
for
(i = start + 1; i <= end; i += 2) {
if
(checkFunc(start, i, st) == 1) {
sum = sum
+ countRec(start + 1, i - 1, dp, st)
* countRec(i + 1, end, dp, st);
}
else
if
(st[start] ==
'?'
&& st[i] ==
'?'
) {
sum = sum
+ countRec(start + 1, i - 1, dp, st)
* countRec(i + 1, end, dp, st)
* 3;
}
}
return
dp[start, end] = sum;
}
static
int
countWays(
string
st)
{
int
n = st.Length;
if
(n % 2 == 1)
return
0;
int
[, ] dp =
new
int
[MAX, MAX];
for
(
int
i = 0; i < MAX; i++)
for
(
int
j = 0; j < MAX; j++)
dp[i, j] = -1;
return
countRec(0, n - 1, dp, st);
}
public
static
void
Main()
{
string
st =
"(?([?)]?}?"
;
Console.WriteLine(countWays(st));
}
}