Palindrome Partitioning与动态规划
2015-09-24 11:39
190 查看
首先看Leetcode上的Palindrome Partitioning题目:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s =
Return
一开始采用的是递归解法:
t存储前n个字符的临时结果。从之前的结果我们可以得到现在的结果。
再来看看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 =
Return
be produced using 1 cut.
类似地,利用上面的动态规划方法,解法如下:
这里,只开辟O(n)的空间存储前n个字符的最少划分次数,然后之后到底有多少个回文串,例如,遍历到字符‘b’时,
s[i] = 'b', 且 s[i-1,i+1]是回文 "aba",如下:
我们知道s[0,i-1)最小划分数是 X,
所以s[0,i+1] 即Y 的最小划分数不会大于X+1. 所以我们需要找到s[0,i+1]中所有的回文串,以找到最小的划分数。代码如下:
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s =
"aab",
Return
[ ["aa","b"], ["a","a","b"] ]
一开始采用的是递归解法:
public class Solution{ List<List<String>> result = new ArrayList<List<String>>(); public List<List<String>> partition(String s) { helper(s, new ArrayList<String>()); return result; } private void helper(String s, List<String> cur){ //DFS every combinations if(s.length() == 0){result.add(cur); return;} for(int i = 1; i <= s.length(); i++){ String sub = s.substring(0,i); if(isPalindromeString(sub)){ List<String> newList = new ArrayList<String>(cur); newList.add(sub); helper(s.substring(i,s.length()), newList); } else continue; //not palindrome, ignore it } } }时间复杂度比较高,因为每个子串都是重新递归,没有保存中间结果。利用动态规划对算法做改进,如下:
public class Solution { public static List<List<String>> partition(String s) { int len = s.length(); List<List<String>>[] result = new List[len + 1]; result[0] = new ArrayList<List<String>>(); result[0].add(new ArrayList<String>()); boolean[][] pair = new boolean[len][len]; for (int i = 0; i < s.length(); i++) { result[i + 1] = new ArrayList<List<String>>(); for (int left = 0; left <= i; left++) { if (s.charAt(left) == s.charAt(i) && (i-left <= 1 || pair[left + 1][i - 1])) { pair[left][i] = true; String str = s.substring(left, i + 1); for (List<String> r : result[left]) { List<String> ri = new ArrayList<String>(r); ri.add(str); result[i + 1].add(ri); } } } } return result[len]; } }其中,pair存储子串(i到j)是否已经是回文串,resul
t存储前n个字符的临时结果。从之前的结果我们可以得到现在的结果。
再来看看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
1since the palindrome partitioning
["aa","b"]could
be produced using 1 cut.
类似地,利用上面的动态规划方法,解法如下:
public class Solution{ public int minCut(String s) { int len = s.length(); boolean[][] pair = new boolean[len][len]; int[] result = new int[len+1]; result[0] = 0; for(int i=0; i<len; i++){ result[i+1] = Integer.MAX_VALUE; for(int left=0; left<=i; left++){ if(s.charAt(left) == s.charAt(i) && (i-left<=1 || pair[left+1][i-1])){ pair[left][i] = true; if(left == 0) result[i+1] = 0; else{ result[i+1] = Math.min(result[left] + 1, result[i+1]); } } } } return result[len]; } }然而此题并不要求给出所有划分的情况,可以考虑减少空间复杂度至O(n)。有一种很巧妙的解法:
这里,只开辟O(n)的空间存储前n个字符的最少划分次数,然后之后到底有多少个回文串,例如,遍历到字符‘b’时,
s[i] = 'b', 且 s[i-1,i+1]是回文 "aba",如下:
.......aba... |<-X->| ^ |<---Y-->|
我们知道s[0,i-1)最小划分数是 X,
所以s[0,i+1] 即Y 的最小划分数不会大于X+1. 所以我们需要找到s[0,i+1]中所有的回文串,以找到最小的划分数。代码如下:
class Solution { public: int minCut(string s) { int n = s.size(); vector<int> cut(n+1, 0); // number of cuts for the first k characters for (int i = 0; i <= n; i++) cut[i] = i-1; for (int i = 0; i < n; i++) { for (int j = 0; i-j >= 0 && i+j < n && s[i-j]==s[i+j] ; j++) // odd length palindrome cut[i+j+1] = min(cut[i+j+1],1+cut[i-j]); for (int j = 1; i-j+1 >= 0 && i+j < n && s[i-j+1] == s[i+j]; j++) // even length palindrome cut[i+j+1] = min(cut[i+j+1],1+cut[i-j+1]); } return cut ; } };
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例