Palindrome Partitioning II
来源:互联网 发布:日语段落翻译软件 编辑:程序博客网 时间:2024/06/07 00:09
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
解法:还比较典型的动态规划,需要注意的是判断子串对偶也用的是动态规划。
定义子问题:dp[i]表示从i到n的子串划分成的回文串的个数,减1即划分的刀数,palin[i][j]表示子串i...j是否为回文串
递归关系:dp[i] = min(dp[i],dp[j+1]+1); i <=j < n
palin[i][j] = pos[i+1][j-1]&&(s[i]==s[j]) || (i == j)
初始条件:dp[i]=n-i;pos[i][j] = false;
class Solution {public: int minCut(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function int len = s.length(); if(len < 2) return 0; bool palin[len][len]; int dp[len+1]; for(int i = 0;i<len;i++) for(int j = 0;j<len;j++) palin[i][j] = false; for(int i = len;i>=0;i--) dp[i] = len - i; for(int i = len - 1; i>=0 ;i--) { for(int j = i;j<len;j++) { if( s[i] == s[j] &&(j - i <2 || palin[i+1][j-1])){ dp[i] = min(dp[i] , 1+ dp[j+1]); palin[i][j] = true; } } } return dp[0]-1; }};124 milli secs.
- 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
- 请问各位大侠,我在应用里面发送短信,想给里面的几个字符加上链接和文字颜色以及下划线,请问怎么做?
- OGG mgr ext rep 参数文件说明
- 了解 Boost 单元测试框架
- oracle 修改编码方式
- OGG oracle DDL双向复制
- Palindrome Partitioning II
- how to truncate a string or get a substring ? js截取字符串
- Android四大组件之Activity篇(一)(Activity的创建、开启和数据传递)
- SVN提交出错--URL access forbidden for unknown reason
- 汇总ExtJS中GridPanel的各个属性与方法
- C++ 排序
- ubuntu 修改主题
- linux 出现丢包解决方法
- phonegap文件,目录操作以及网络上传,下载文件(含demo)