Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P7914 [CSP-S 2021] 括号序列

P7914 [CSP-S 2021] 括号序列

考虑区间DP统计答案,

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstdio>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 510, mod = 1e9 + 7;
char s[N];
int n, m;
void adj (int &x) { x += x >> 31 & mod; }
struct ModInt
{
int x;
ModInt (int _ = 0) { adj(x = _); }
int operator () () const { return x; }
ModInt& operator += (const ModInt &_) { adj(x += _.x - mod); return *this; }
ModInt& operator -= (const ModInt &_) { adj(x -= _.x); return *this; }
ModInt& operator *= (const ModInt &_) { x = (LL)x * _.x % mod; return *this; } ModInt operator + (const ModInt &_) const { ModInt res = x; res += _; return res; }
ModInt operator - (const ModInt &_) const { ModInt res = x; res -= _; return res; }
ModInt operator * (const ModInt &_) const { ModInt res = x; res *= _; return res; }
} f[N][N][6];
int main ()
{
read(n, m);
scanf("%s", s + 1);
for (int i = 1; i <= n; ++i) f[i][i - 1][0] = 1;
for (int k = 0; k < n; ++k)
for (int i = 1, j = i + k; j <= n; ++i, ++j)
{
if (k + 1 <= m && (s[j] == '*' || s[j] == '?'))
f[i][j][0] = f[i][j - 1][0];
if (k)
{
if ((s[i] == '(' || s[i] == '?') && (s[j] == ')' || s[j] == '?'))
f[i][j][1] = f[i + 1][j - 1][0] + f[i + 1][j - 1][2] + f[i + 1][j - 1][3] + f[i + 1][j - 1][4];
for (int t = i; t < j; ++t)
{
f[i][j][2] += f[i][t][3] * f[t + 1][j][0];
f[i][j][3] += (f[i][t][2] + f[i][t][3]) * f[t + 1][j][1];
f[i][j][4] += (f[i][t][4] + f[i][t][5]) * f[t + 1][j][1];
f[i][j][5] += f[i][t][4] * f[t + 1][j][0];
}
}
f[i][j][5] += f[i][j][0];
f[i][j][3] += f[i][j][1];
}
write(f[1][n][3]());
return 0;
}