[leetcode][DP] Palindrome Partitioning II
2015-07-01 11:12
531 查看
题目;
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 =
Return
be produced using 1 cut.
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
1since the palindrome partitioning
["aa","b"]could
be produced using 1 cut.
class Solution { public: int minCut(string s) { int n = s.size(); vector<int> table2(n);//table2[i]表示从s[i]到s[n-1]分成palindrom最少需要切几刀 vector<bool> dummy(n, false); vector<vector<bool> > table1(n);//table[i][j]表示s[i]...s[j]能否构成回文 for (int i = 0; i < n; ++i){ table2[i] = n - i - 1; } for (int i = 0; i < n; ++i){ table1[i] = dummy; } for (int i = n - 2; i >= 0; --i){ for (int j = i; j < n; ++j){ if (i == j || s[i] == s[j] && (j == i + 1 || table1[i + 1][j - 1])){ table1[i][j] = true; if (j == n - 1) table2[i] = 0; else table2[i] = table2[j + 1] + 1 < table2[i] ? table2[j + 1] + 1 : table2[i]; } } } return table2[0]; } };
相关文章推荐
- socket通信常用的函数
- 大数相加相减相乘
- [leetcode] Surrounded Regions
- Android主题应用
- 文章标题
- sublime text3空格和tab的显示
- 电影 杭州景点
- 动态添加删除css/js文件
- JDBC
- uleb128、sleb128和uleb128p1编码格式介绍
- iOS IAP 自动更新的订阅服务
- IndexDB 使用问题
- Spring MVC拦截器+注解方式实现防止表单重复提交
- jQuery 中获取调用Tab的当前页面
- 上课知识
- Tomcat内存设置详解
- 《Java课程实习》日志(周二)
- POJ3229
- centos6.4安装kvm
- python实现的希尔排序算法实例