Palindrome Partitioning II
来源:互联网 发布:帝国cms好吗 编辑:程序博客网 时间:2024/06/06 11:42
Palindrome Partitioning II
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.
f(i,j) 表示[i,j]之前最小的cut数,状态转移方程:
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.
f(i,j) 表示[i,j]之前最小的cut数,状态转移方程:
f(i,j) = min{f(i,k) + f(k+1,j)} i<=k<=j, 0<=i <=j <n
这是一个二维函数,所以我们转化为一维DP
f(i) 表示[i, n-1] 之间最小的cut数, n为字符串长度,则状态转移方程为
f(i) = min {f(j+1) +1} , i<=j < n
判断[i,j]是否是回文
P[i,j] = true if [i,j]是回文, 那么
P[i][j] = str[i] == str[j] && P[i+1][j-1]
class Solution {public: int minCut(string s) { const int n = s.size(); int f[n+1]; bool p[n][n]; fill_n(&p[0][0], n*n, false); for (int i = 0; i <=n; i++) { f[i] = n-1-i; } for (int i = n-1; i >=0; i--) { for (int j = i; j < n; j++) { if (s[i] == s[j] && (j-i < 2 || p[i+1][j-1])) { p[i][j] = true; f[i] = min(f[i], f[j+1]+1); } } } return f[0]; }};
0 0
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- Palindrome Partitioning II
- Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- Palindrome Partitioning II
- LetCode:Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- ArcGIS制图——多图层道路压盖处理
- OpenCV获取与设置像素点的值的几个方法
- 阅读程序5、6
- android 进程查看
- 1.4.2工作原理章节
- Palindrome Partitioning II
- yslow 应用
- SQL必知必会 笔记 第三章 排序检索数据
- stm32串口DMA收发,可以接收不定长数据,格式化输出。
- mac QQ 截图失效 解决方案
- 异常点检测算法分析与选择
- vc调试基本总结
- 如何获得和修改文件的时间
- ubuntu下挂载win7共享目录