【leetcode】Palindrome Partitioning && Palindrome Partitioning II
2014-07-25 17:33
483 查看
Palindrome Partitioning
链接:https://oj.leetcode.com/problems/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 = "aab"
,
Return
[
["aa","b"],
["a","a","b"]
]
解法:dfs搜索所有解,同时借助dp[i][j]计算从i个字符到第j个字符是否回文。
代码如下:
vector<vector<string>> partition(string s) { vector<vector<string>> result; int len = s.length(); if( len <= 0) return result; vector<vector<bool> > dp(len, vector<bool>(len, false)); for(int i = 0; i < len; ++i) dp[i][i] = true; for(int r = 2; r <= len; ++r) { for(int i=0; i <= len - r;++i) { int j = i+r-1; if( s[i] == s[j] && ( i+1 >= j-1 || dp[i+1][j-1]) ) dp[i][j] = true; } } vector<string> path; dfs(s, 0, dp, path, result); return result; } void dfs(string &s, int start, vector<vector<bool>> &dp, vector<string> &path, vector<vector<string>> &result) { int len = s.length(); if(start == len) { result.push_back(path); return; } for(int i=start; i < len; ++i) { if( dp[start][i] ){ path.push_back(s.substr(start, i - start + 1)); dfs(s, i+1, dp, path, result); path.pop_back(); } } }
Palindrome
Partitioning II
链接:https://oj.leetcode.com/problems/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.
方法一:
两个DP,结果为了图省事,写了一个O(n^3)。超时
代码如下:
int minCut(string s) { int len = s.length(); if( len <= 1) return 0; vector<vector<int>> dp(len, vector<int>(len, INT_MAX)); for(int i=0;i < len; ++i) dp[i][i] = 0; for(int r=2; r <= len; ++r) { for(int i=0; i <= len - r;++i) { int j = i + r - 1; if(s[i] == s[j] && (i+2 >= j || dp[i+1][j-1] == 0)){ dp[i][j] = 0; continue; } for(int m=i; m < j; ++m) { int temp = 1 + dp[i][m] + dp[m+1][j]; if( dp[i][j] > temp) dp[i][j] = temp; } } } return dp[0][len-1]; }
方法二:
拆分DP,将第二个DP转化为一位DP。
DP[i] = 0 if flag[0][i] == true
DP[i] = min { DP[j] + flag[j+1][i] ? 1 : (i-j) } 0 <= j < i; else
进一步分析 当flag[j+1][i] 为真时更新就能保证正确性。
代码如下:
int minCut2(string s) { int len = s.length(); if(len <= 1) return 0; vector<vector<bool>> dp(len, vector<bool>(len, false)); for(int i=0;i < len; ++i) dp[i][i] = true; for(int r=2; r <= len; ++r) { for(int i=0; i <= len-r; ++i) { int j = r + i - 1; if( s[i] == s[j] && (i + 2 >= j || dp[i+1][j -1])) dp[i][j] = true; } } vector<int> output(len, 0); for(int i=1;i < len; ++i) { if( dp[0][i] ){ output[i] = 0; continue; }else{ output[i] = i; } for(int j = i; j > 0; --j) { if( dp[j][i] ) output[i] = min(output[i], output[j-1] + 1); } } return output[len-1]; }
相关文章推荐
- [Leetcode] #131#132 Palindrome Partitioning I & II
- LeetCode - Palindrome Partitioning I && II
- Leetcode: palindrome-partitioning && palindrome-partitioning-ii
- leetcode -day11 Clone Graph & Palindrome Partitioning I II
- LeetCode Palindrome Partitioning I&&Palindrome Partitioning II
- leetcode之 Palindrome Partitioning I&II
- 【LeetCode】palindrome-partitioning i&ii
- [LeetCode] Palindrome Partitioning && Palindrome Partitioning II
- Leetcode: Palindrome Partitioning I & II
- LeetCode132 Palindrome Partitioning II&I
- leetcode之 Palindrome Partitioning I&II
- Leetcode 131&132. Palindrome Partitioning I & II
- Leetcode:Palindrome Partitioning & Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning I&II
- 【LeetCode】Palindrome Partitioning I && II
- leetcode之 Palindrome Partitioning I&II
- leetcode之 Palindrome Partitioning I&II
- 【LeetCode】Palindrome Partitioning I&II
- [leetcode]Palindrome Partitioning II
- LeetCode之Palindrome Partitioning II