-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPalindrome Partitioning II.cpp
58 lines (52 loc) · 1.32 KB
/
Palindrome Partitioning II.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
//Given a string A, partition A such that every substring of the partition is a palindrome.
//Return the minimum cuts needed for a palindrome partitioning of A.
//1 <= length(A) <= 501
// Input 1:
// A = "aba"
// Output 1:
// 0
// Explanation 1:
// "aba" is already a palindrome, so no cuts are needed.
// Input 2:
// A = "aab"
// Output 2:
// 1
// Explanation 2:
// Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
//Code:
int static t[502][502];
bool isPalindrome(string s,int i,int j){
if(i>=j)return true;
while(i<j){
if(s[i]!=s[j])return false;
i++;j--;
}
return true;
}
int solve(string s,int i,int j){
if(i>=j)return 0;
if(isPalindrome(s,i,j)==true)return 0;
if(t[i][j]!=-1)return t[i][j];
int mn = INT_MAX;
for(int k=i;k<=j-1;k++){
int left,right;
if(t[i][k]!=-1)left = t[i][k];
else {
left = solve(s,i,k);
t[i][k]=left;
}
if(t[k+1][j]!=-1)right = t[k+1][j];
else {
right = solve(s,k+1,j);
t[k+1][j] = right;
}
int tmp = 1+left+right;
mn = min(mn,tmp);
}
return t[i][j]=mn;
}
int Solution::minCut(string A) {
int l = A.length();
memset(t,-1,sizeof(t));
return solve(A,0,l-1);
}