-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1618.cpp
More file actions
77 lines (63 loc) · 1.69 KB
/
1618.cpp
File metadata and controls
77 lines (63 loc) · 1.69 KB
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
68
69
70
71
72
73
74
75
76
77
// https://acm.timus.ru/problem.aspx?space=1&num=1618
// aliasing (reference identity) + parsing deepToString brackets + equivalence relations / partitions + stirling numbers (2nd kind) + DP over levels + modular arithmetic
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define rep(i,a,b) for (int i = (a); i <= (b); ++i)
#define pb push_back
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
const int MOD = 10007;
string s;
getline(cin, s);
vi st;
vi len(2, -1);
int maxd = 0;
for (char c : s)
{
if (c == '[')
{
st.pb(0);
maxd = max(maxd, sz(st));
}
else if (c == ']')
{
int cnt = st.back();
st.pop_back();
int depth = sz(st) + 1;
if (sz(len) <= depth) len.resize(depth + 1, -1);
if (len[depth] == -1) len[depth] = cnt;
if (!st.empty()) st.back()++;
}
}
int D = maxd;
vi a;
rep(d, 1, D - 1) a.pb(len[d]);
int N = 512;
vvi S(N + 1, vi(N + 1, 0));
S[0][0] = 1;
rep(n, 1, N) rep(k, 1, n) S[n][k] = (S[n - 1][k - 1] + (ll)k * S[n - 1][k]) % MOD;
vi dp(N + 1, 0), ndp(N + 1, 0);
dp[1] = 1;
for (int x : a)
{
fill(all(ndp), 0);
rep(b, 1, N)
{
if (!dp[b]) continue;
int q = b * x;
rep(c, 1, q) ndp[c] = (ndp[c] + (ll)dp[b] * S[q][c]) % MOD;
}
dp.swap(ndp);
}
int ans = 0;
rep(b, 1, N) ans = (ans + dp[b]) % MOD;
cout << ans % MOD << '\n';
return 0;
}