-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2472.cpp
29 lines (28 loc) · 920 Bytes
/
2472.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
class Solution {
public:
int maxPalindromes(string s, int k) {
int n = s.size();
vector<vector<bool>> dp(n, vector<bool>(n, false));
for (int i = 0; i < n; ++i) {
dp[i][i] = true;
}
for (int length = 2; length <= n; ++length) {
for (int i = 0; i < n; ++i) {
int end = i + length - 1;
if (end >= n) break;
if (s[i] == s[end]) {
if (i + 1 == end) dp[i][end] = true;
else dp[i][end] = dp[i + 1][end - 1];
}
}
}
vector<int> setDp(n + 1, 0);
for (int i = 1; i <= n; ++i) {
setDp[i] = setDp[i - 1];
for (int j = 1; j <= i; ++j) {
if (dp[j - 1][i - 1] && i - j + 1 >= k) setDp[i] = max(setDp[i], setDp[j - 1] + 1);
}
}
return setDp[n];
}
};