Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could
be produced using 1 cut.
class Solution {
public:
int minCut(string s) {
if(s.length()==0||s.length()==1) {
return 0;
}
vector<vector<int>> palindrome_map(s.length(), vector<int>(s.length()));
vector<int> cut_num_array(s.length() + 1);
for(int ii=s.length() - 1; ii >= 0;ii --) {
cut_num_array[ii] = s.length() - ii;
for(int jj = ii; jj < s.length(); jj ++) {
if(s[ii] == s[jj]) {
if(jj - ii < 2 || palindrome_map[ii + 1][jj - 1] == 1) {
palindrome_map[ii][jj] = 1;
cut_num_array[ii] = min(cut_num_array[ii], cut_num_array[jj + 1] + 1);
}
}
}
}
return cut_num_array[0] - 1;
}
};
本文介绍了一种算法,用于计算将给定字符串分割成多个回文子串所需的最少切割次数。通过动态规划的方法,建立了回文判断矩阵,并计算出最优切割数量。
324

被折叠的 条评论
为什么被折叠?



