-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK. Palíndromo.cpp
84 lines (64 loc) · 1.25 KB
/
K. Palíndromo.cpp
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
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
string s;
vector<int> marked;
int n, m;
struct st {
int sz, mcnt;
st():sz(0), mcnt(0) {};
bool operator <(const st b) const {
return mcnt == b.mcnt ? sz < b.sz : mcnt < b.mcnt;
}
bool operator > (const st b) const {
return mcnt == b.mcnt ? sz > b.sz : mcnt > b.mcnt;
}
};
const int maxn = 2000;
vector<vector<st>> memo(maxn, vector<st>(maxn));
st solve(int l, int r) {
if (l > r) {
return st();
}
auto &[sz, mcnt] = memo[l][r];
if (sz or mcnt) return memo[l][r];
if (l == r) {
sz = 1;
mcnt = marked[l];
return memo[l][r];
}
if (s[l] == s[r]) {
sz = 2;
mcnt = marked[l] + marked[r];
auto tmp = solve(l+1, r-1);
memo[l][r] = max(memo[l][r], tmp);
tmp.sz += 2;
tmp.mcnt += marked[l] + marked[r];
memo[l][r] = max(memo[l][r], tmp);
}
{
auto tmp = solve(l+1, r);
memo[l][r] = max(memo[l][r], tmp);
}
{
auto tmp = solve(l, r-1);
memo[l][r] = max(memo[l][r], tmp);
}
return memo[l][r];
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
n = s.size();
marked.resize(n);
int m;
cin >> m;
for (int i = 0, p; i < m; i++) {
cin >> p;
p--;
marked[p] = 1;
}
auto [ans, _] = solve(0, n-1);
cout << ans << '\n';
}
// AC, dp, strings