File tree Expand file tree Collapse file tree 1 file changed +89
-0
lines changed
daily_problems/2024/03/0328/personal_submission Expand file tree Collapse file tree 1 file changed +89
-0
lines changed Original file line number Diff line number Diff line change 1+ struct AC {
2+ struct node {
3+ vector<int > child;
4+ int fail{};
5+ int sums{};
6+ node () {
7+ child.resize (26 );
8+ fail = 0 , sums = 0 ;
9+ }
10+ };
11+ vector<node> tree;
12+ int tot = 2 ;
13+ explicit AC (int n) : tree(n) {
14+ tot = 2 ;
15+ ranges::fill (tree[0 ].child , 1 );
16+ }
17+
18+ void insert (string&s,int k) {
19+ int now = 1 ;
20+ for (auto j: s) {
21+ int i = j - ' a' ;
22+ if (!tree[now].child [i]) {
23+ tree[now].child [i] = tot++;
24+ }
25+ now = tree[now].child [i];
26+ }
27+ tree[now].sums += k;
28+ }
29+
30+ void build () {
31+ queue<int > q;
32+ q.emplace (1 );
33+ vector<int > ans;
34+ while (!q.empty ()) {
35+ auto t = q.front ();
36+ ans.emplace_back (t);
37+ q.pop ();
38+ int fail = tree[t].fail ;
39+ for (int i = 0 ; i < 26 ; i++) {
40+ if (!tree[t].child [i]) {
41+ tree[t].child [i] = tree[fail].child [i];
42+ }
43+ else {
44+ int next = tree[t].child [i];
45+ tree[next].fail = tree[fail].child [i];
46+ q.emplace (next);
47+ }
48+ }
49+ }
50+ for (auto i: ans) {
51+ tree[i].sums += tree[tree[i].fail ].sums ;
52+ }
53+ }
54+
55+ int rundp (string&s) {
56+ int n = s.length ();
57+ vector f (n + 10 , vector (tot + 10 , -inf));
58+ f[0 ][1 ] = 0 ;
59+ for (int i = 0 ; i < n; i++) {
60+ for (int j = 0 ; j < tot; j++) {
61+ if (f[i][j] != -inf) {
62+ for (int k = 0 ; k < 26 ; k++) {
63+ if (k + ' a' == s[i] || s[i] == ' *' ) {
64+ int to = tree[j].child [k];
65+ f[i + 1 ][to] = max (f[i + 1 ][to], f[i][j] + tree[to].sums );
66+ }
67+ }
68+ }
69+ }
70+ }
71+ int maxs = -inf;
72+ for (int i = 0 ; i < tot; i++) {
73+ maxs = max (f[n][i], maxs);
74+ }
75+ return maxs;
76+ }
77+ };
78+
79+
80+ void solve () {
81+ AC a (1000 );
82+ string s, s1, s2;
83+ cin >> s >> s1 >> s2;
84+ a.insert (s1, 1 );
85+ a.insert (s2, -1 );
86+ a.build ();
87+ int ans = a.rundp (s);
88+ cout << ans << endl;
89+ }
You can’t perform that action at this time.
0 commit comments