LeetCode131: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"]
- ]
- 解题思路:
- 这道题跟Word Break II这道基本一样,需要用DP+DFS解决
这里采用DP中的自底向上实现,dp[i]表示前i个字符是否为切分为多个回文字符串。当求解dp[i]时,可利用已经求解的dp[i-1],
dp[i-2]…dp[1],dp[0]进行求解。
对于dp[n]的求解,我们可以将n个字符进行切分求解,分为前i个字符和后n-i个字符,i可以为(0,1,2,3,4…n-1)
假设i为1时,可根据dp[i]和后面的n-1个字符组成的单词是否在dict中来判断dp[n],只要i(0,1,2,3,4…n-1)其中一种
情况为真,则dp[n]为true,表示可以进行切分为多个回文字符串。
因为本题需要重构结果,所以必须要有一个数据结构来保存每段长度的切割方案,这里我用unordered_map<int, vector<int> >进行保存,key为字符长度,vector保存该key对应的切割方案。
如何求得unordered_map<int, vector<int> >中的值呢?那就应该利用求解dp[i]时,每当有一种切割方案使得dp[i]为true时,将其对应的切割位置存放到i对应的vector中,待之后用于结果重构。
unordered_map<int, vector<int> >求得后,接下来是采用DFS算法进行结果重构,如代码执行结果图,当长度为3时,有一种切割方案,即在长度为2的位置进行切割,然后长度为2的切割方案又有两种0和1,长度为1时,切割方案都为0,所以采用DFS时,遍历顺序为2,0然后获得一种结果,之后回溯到2,1,因为1的切割方案为0,所以为3,1,0,又是一种结果。
实现代码:
- #include <iostream>
- #include <vector>
- #include <iterator>
- #include <unordered_map>
- #include <string>
- #include <algorithm>
- using namespace std;
- /*
- 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"]
- ]
- */
- class Solution {
- public:
- //DP
- vector<vector<string>> partition(string s) {
- vector<vector<string>> retvec;
- if(s.size() == 0)
- return retvec;
- int len = s.size();
- vector<bool> dp(len+1, false);//前i个字符是否为回文数
- dp[0] = true;
- unordered_map<int, vector<int>> hashtable;//对前i个字符,如果其为回文数时的切分点
- for(int i = 1; i <= len; i++)
- {
- vector<int> tmpv;
- for(int j = 0; j < i; j++)
- {
- if(dp[j] && isPalindrome(s.substr(j, i-j)) )
- {
- dp[i] = true;
- tmpv.push_back(j);
- }
- }
- hashtable[i] = tmpv;
- }
- for(int k = 1; k <= len; k++)
- {
- vector<int> tvec = hashtable[k];
- cout<<k<<":";
- copy(tvec.begin(), tvec.end(), ostream_iterator<int>(cout, " "));
- cout<<endl;
- }
- vector<int> curvec;
- getResult(retvec, hashtable, s, len, curvec);
- return retvec;
- }
- bool isPalindrome(string s)
- {
- int len = s.size();
- if(len == 0)
- return false;
- for(int i = 0; i <= len/2; i++)
- if(s[i] != s[len-i-1])
- return false;
- return true;
- }
- //DFS
- void getResult(vector<vector<string>> &retvec, unordered_map<int, vector<int>> &hashtable, string &s, int len, vector<int> &curvec)
- {
- if(len == 0)
- {
- vector<string> tv;
- int start = curvec.back();
- for(int i = curvec.size()-2; i >= 0; i--)
- {
- int c = curvec[i];
- tv.push_back(s.substr(start, c-start));
- start = c;
- }
- tv.push_back(s.substr(curvec[0]));
- retvec.push_back(tv);
- return ;
- }
- vector<int> tmpv = hashtable[len];
- vector<int>::iterator iter;
- for(iter = tmpv.begin(); iter != tmpv.end(); ++iter)
- {
- curvec.push_back(*iter);
- getResult(retvec, hashtable, s, *iter, curvec);
- curvec.pop_back();
- }
- }
- };
- int main(void)
- {
- string s("aab");
- Solution solution;
- vector<vector<string>> retvv = solution.partition(s);
- vector<vector<string>>::iterator iter;
- for(iter = retvv.begin(); iter != retvv.end(); ++iter)
- {
- vector<string>::iterator it;
- for(it = (*iter).begin(); it != (*iter).end(); ++it)
- cout<<*it<<" ";
- cout<<endl;
- }
- return 0;
- }
运行结果:
LeetCode131:Palindrome Partitioning的更多相关文章
- Leetcode131. Palindrome Partitioning分割回文串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
- LintCode Palindrome Partitioning II
Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...
- LeetCode(131)Palindrome Partitioning
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- Leetcode 131. Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- JavaScriptSerializer 中的匿名类型 转json
二:JavaScriptSerializer 中的匿名类型 这个类型我想大家都清楚,不过性能更高的方式应该是用JsonConvert吧,但这个不是本篇讨论的话题,我们重点来看看匿名类型的Json序列化 ...
- njoj 1251 zlly长了一张包子脸
njoj 1251 zlly长了一张包子脸 题意: zlly长了一张包子脸.他特别喜欢吃糖果.如今他手头有若干种糖果,每种糖果有个口味值,每种糖果有无数多个.然后娄童鞋也很喜欢吃糖果.他的口味特别广泛 ...
- OBS---环境配置之#include <D3DX10.h>报错
一.先贴错误 因为这个笔记主要记录我如何整好这个OBS源码环境的,给需要的童鞋一个参考 1.1.#include <D3DX10.h> 报错 没有这个 解决方案:把2,3先解决了就水到渠 ...
- WorkbookDesigner mvc里面返回file
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- table变宽格式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 未知高度定宽div水平居中及垂直居中(兼容ie6及其他牛逼浏览器)
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByTagName
按照标签名获取元素 -- getElementsByTagName 标准 DOM 1在Element和Document两个interface中均有定义,原型NodeList getElementsBy ...
- 二十三、【开源】EFW框架Web前端开发之常用组件(FusionCharts图表、ReportAll报表等)
回<[开源]EFW框架系列文章索引> EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...
- Code the Tree(图论,树)
ZOJ Problem Set - 1097 Code the Tree Time Limit: 2 Seconds Memory Limit: 65536 KB A tree (i.e. ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...