-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1278.分割回文串-iii.cpp
63 lines (51 loc) · 1.44 KB
/
1278.分割回文串-iii.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
#include "s.h"
/*
* @lc app=leetcode.cn id=1278 lang=cpp
*
* [1278] 分割回文串 III
*/
// @lc code=start
class Solution {
public:
int palindromePartition(string s, int k) {
int n = s.size();
if(n==k) return 0;
vector<vector<vector<int>>> dp(k+1, vector<vector<int>>(n+1,vector<int>(n+1,INT_MAX)));
for(int i=0;i<n;i++){
dp[1][i][i]=0;
}
for(int j=n;j-->0;){
for(int i=j+1;i<n;i++){
if(i-j>1){
if(s[j]==s[i]){
dp[1][j][i] = dp[1][j+1][i-1];
}else{
dp[1][j][i] = dp[1][j+1][i-1]+1;
}
}else{
if(s[j]==s[i]){
dp[1][j][i] = 0;
}else{
dp[1][j][i] = 1;
}
}
}
}
for(int i=2;i<=k;i++){
for(int j=n-i;j>=0;j--){
int o=1;
for(int l=j+i-1;l<n;l++){
for(int ll=j+o-1;l-ll>=i-o;ll++){
dp[i][j][l] = min(dp[i][j][l], dp[o][j][ll]+dp[i-o][ll+1][l]);
}
}
}
}
return dp[k][0][n-1];
}
};
// @lc code=end
int main(){
Solution s;
cout << s.palindromePartition("adfasdf", 5);
}