-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path1278.cpp
31 lines (30 loc) · 793 Bytes
/
1278.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
class Solution
{
public:
int palindromePartition(string s, int k)
{
int n = s.size();
int p[n][n] = {}, f[k+1][n] = {};
for (int j = 1; j < n; j++)
{
for (int i = j - 1; i >= 0; i--)
{
p[i][j] = s[i] == s[j] ? 0 : 1;
if (i + 1 < j) p[i][j] += p[i+1][j-1];
}
}
memset(f, 0x3f, sizeof f);
for (int i = 0; i < n; i++) f[1][i] = p[0][i];
for (int i = 2; i <= k; i++)
{
for (int r = i - 1; r < n; r++)
{
for (int l = i - 2; l < r; l++)
{
f[i][r] = min(f[i][r], f[i-1][l] + p[l+1][r]);
}
}
}
return f[k][n-1];
}
};