最小回文分割数 Palindrome Partitioning II
来源:互联网 发布:tensorflow matlab 编辑:程序博客网 时间:2024/05/16 12:39
问题: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.
与之的类似问题是《回文子串划分 Palindrome Partitioning》。
思路:两次动态规划。
第一次DP,找到字符串的任意子串是否是回文,状态量H[i][j]记录子串s[i..j]是否是回文。
其递推关系,可参考http://blog.csdn.net/ojshilu/article/details/12155529
第二次DP,找到任意前缀子串的最小回文分割数,状态量P[i]记录前缀子串s[0..i]的最小分割数。
其递推关系,若H[0][i] = 1,那么P[i] = 0,否则 P[i] = min{P[k] +1 when H[k+1][i] = 1}。
class Solution {public: int minCut(string s) { int n = s.size(); if(n < 2) return 0; //第一次动态规划:找出任意子串是否是回文 int **H = new int *[n]; // H[i][j]表示str[i..j]是否是回文 for(int i=0;i<n;i++) { H[i] = new int[n]; memset(H[i], 0, sizeof(H[i])); } for(int i=0;i<n;i++) H[i][i] = 1; for(int i=0;i<n-1;i++) if(s[i] == s[i+1]) H[i][i+1] = 1; else H[i][i+1] = 0; for(int m=3;m<=n;m++) { for(int i=0;i<n-m+1;i++) { int j = i + m - 1; if(s[i] == s[j] && H[i+1][j-1] == 1) H[i][j] = 1; else H[i][j] = 0; } } if(H[0][n-1] == 1) return 0; //第二次动态规划:找出所有前缀子串的最小分割数 int P[n]; //P[i]表示str[0..i]的最小分割数 memset(P, 0, sizeof(P)); P[0] = 0; for(int i=1;i<n;i++) { if(H[0][i] == 1) { P[i] = 0; continue; } int min = P[i-1] + 1; for(int j=i-1;j>0;j--) { if(H[j][i] == 1 && P[j-1] + 1 < min) min = P[j-1] + 1; } P[i] = min; } return P[n-1]; }};
0 0
- 最小回文分割数 Palindrome Partitioning II
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- 动态规划——回文最小分割数(palindrome-partitioning-ii)
- Palindrome Partitioning II 最小的括号分割
- [LeetCode] Palindrome Partitioning 回文分割
- Palindrome Partitioning II 最少的回文数切割
- Palindrome Partitioning&II(回文字符串)
- 108.Palindrome Partitioning II-分割回文串 II(中等题)
- Palindrome Partitioning 分割字符串为回文@LeetCode
- Leetcode 131 Palindrome Partitioning 回文分割
- Palindrome Partitioning 分割成回文子字符串
- [Lintcode]Palindrome Partitioning分割回文串
- Palindrome Partitioning II(找给定字符串分割次数获取回文字串, 动态规划)
- Palindrome Partitioning 回文串分割,求所有可能的分割
- LeetCode(Palindrome Partitioning) 将字符串分割成回文字符串
- [LeetCode]-Palindrome Partitioning 求回文分割的所有情况
- LeetCode OJ 之 Palindrome Partitioning (字符串的回文分割)
- leetcode之Sort List
- Button的使用(四):获取RadioGroup的值
- java中Executor接口
- 四元数与欧拉角之间的转换
- awr导出分析数据【转自春哥】
- 最小回文分割数 Palindrome Partitioning II
- hdu2087 KMP+简单动归
- 学习servlet ——tomcat安装与启动
- 泛化误差,交叉验证和特征选择
- 语言编程需要注意的64位和32机器的区别
- 论window和Linux之长短 王垠
- 趣文:如何向外行解释机器学习和数据挖掘
- 完全退出程序,退出所有activity
- 你用哪种工具进行iOS app自动化功能测试?