题目:

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

  1. [
  2. ["aa","b"],
  3. ["a","a","b"]
  4. ]
  1. 解题思路:
  1. 这道题跟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,又是一种结果。

实现代码:

  1. #include <iostream>
  2. #include <vector>
  3. #include <iterator>
  4. #include <unordered_map>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9. /*
  10. Given a string s, partition s such that every substring of the partition is a palindrome.
  11.  
  12. Return all possible palindrome partitioning of s.
  13.  
  14. For example, given s = "aab",
  15. Return
  16.  
  17. [
  18. ["aa","b"],
  19. ["a","a","b"]
  20. ]
  21. */
  22. class Solution {
  23. public:
  24. //DP
  25. vector<vector<string>> partition(string s) {
  26. vector<vector<string>> retvec;
  27. if(s.size() == 0)
  28. return retvec;
  29. int len = s.size();
  30. vector<bool> dp(len+1, false);//前i个字符是否为回文数
  31. dp[0] = true;
  32. unordered_map<int, vector<int>> hashtable;//对前i个字符,如果其为回文数时的切分点
  33. for(int i = 1; i <= len; i++)
  34. {
  35. vector<int> tmpv;
  36. for(int j = 0; j < i; j++)
  37. {
  38. if(dp[j] && isPalindrome(s.substr(j, i-j)) )
  39. {
  40. dp[i] = true;
  41. tmpv.push_back(j);
  42.  
  43. }
  44. }
  45. hashtable[i] = tmpv;
  46. }
  47.  
  48. for(int k = 1; k <= len; k++)
  49. {
  50. vector<int> tvec = hashtable[k];
  51. cout<<k<<":";
  52. copy(tvec.begin(), tvec.end(), ostream_iterator<int>(cout, " "));
  53. cout<<endl;
  54. }
  55. vector<int> curvec;
  56. getResult(retvec, hashtable, s, len, curvec);
  57. return retvec;
  58.  
  59. }
  60. bool isPalindrome(string s)
  61. {
  62. int len = s.size();
  63. if(len == 0)
  64. return false;
  65. for(int i = 0; i <= len/2; i++)
  66. if(s[i] != s[len-i-1])
  67. return false;
  68. return true;
  69. }
  70.  
  71. //DFS
  72. void getResult(vector<vector<string>> &retvec, unordered_map<int, vector<int>> &hashtable, string &s, int len, vector<int> &curvec)
  73. {
  74. if(len == 0)
  75. {
  76. vector<string> tv;
  77. int start = curvec.back();
  78. for(int i = curvec.size()-2; i >= 0; i--)
  79. {
  80. int c = curvec[i];
  81. tv.push_back(s.substr(start, c-start));
  82. start = c;
  83. }
  84. tv.push_back(s.substr(curvec[0]));
  85. retvec.push_back(tv);
  86. return ;
  87. }
  88. vector<int> tmpv = hashtable[len];
  89. vector<int>::iterator iter;
  90. for(iter = tmpv.begin(); iter != tmpv.end(); ++iter)
  91. {
  92. curvec.push_back(*iter);
  93. getResult(retvec, hashtable, s, *iter, curvec);
  94. curvec.pop_back();
  95.  
  96. }
  97.  
  98. }
  99. };
  100.  
  101. int main(void)
  102. {
  103. string s("aab");
  104. Solution solution;
  105. vector<vector<string>> retvv = solution.partition(s);
  106. vector<vector<string>>::iterator iter;
  107. for(iter = retvv.begin(); iter != retvv.end(); ++iter)
  108. {
  109. vector<string>::iterator it;
  110. for(it = (*iter).begin(); it != (*iter).end(); ++it)
  111. cout<<*it<<" ";
  112. cout<<endl;
  113. }
  114.  
  115. return 0;
  116. }

运行结果:

LeetCode131:Palindrome Partitioning的更多相关文章

  1. Leetcode131. Palindrome Partitioning分割回文串

    给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...

  2. LeetCode 131. 分割回文串(Palindrome Partitioning)

    131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...

  3. [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  4. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

  7. LintCode Palindrome Partitioning II

    Given a string s, cut s into some substrings such that every substring is a palindrome. Return the m ...

  8. LeetCode(131)Palindrome Partitioning

    题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  9. Leetcode 131. Palindrome Partitioning

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

随机推荐

  1. JavaScriptSerializer 中的匿名类型 转json

    二:JavaScriptSerializer 中的匿名类型 这个类型我想大家都清楚,不过性能更高的方式应该是用JsonConvert吧,但这个不是本篇讨论的话题,我们重点来看看匿名类型的Json序列化 ...

  2. njoj 1251 zlly长了一张包子脸

    njoj 1251 zlly长了一张包子脸 题意: zlly长了一张包子脸.他特别喜欢吃糖果.如今他手头有若干种糖果,每种糖果有个口味值,每种糖果有无数多个.然后娄童鞋也很喜欢吃糖果.他的口味特别广泛 ...

  3. OBS---环境配置之#include <D3DX10.h>报错

    一.先贴错误 因为这个笔记主要记录我如何整好这个OBS源码环境的,给需要的童鞋一个参考 1.1.#include <D3DX10.h>  报错 没有这个 解决方案:把2,3先解决了就水到渠 ...

  4. WorkbookDesigner mvc里面返回file

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. table变宽格式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. 未知高度定宽div水平居中及垂直居中(兼容ie6及其他牛逼浏览器)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. 跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByTagName

    按照标签名获取元素 -- getElementsByTagName 标准 DOM 1在Element和Document两个interface中均有定义,原型NodeList getElementsBy ...

  8. 二十三、【开源】EFW框架Web前端开发之常用组件(FusionCharts图表、ReportAll报表等)

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.2:http://pan.baidu.com/s/1hcnuA EFW框架实例源代码下载:http://pan ...

  9. Code the Tree(图论,树)

    ZOJ Problem Set - 1097 Code the Tree Time Limit: 2 Seconds      Memory Limit: 65536 KB A tree (i.e. ...

  10. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...