题目:

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的更多相关文章

  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. Apache+PHP+Mysql OS X 10.9 Mavericks WEB 服务器配置

    在 OS X 10.9 上基本没有什么特别大的差异. 为了新系统用户方便小弟重新整理了一下,因为在 OSX 10.9 下的 Server 软件进行了不少升级,有些步骤不太一样了. 硬件方面就不在详细描 ...

  2. CKEditor4.1和CKFinder2.3.1 for Mvc4最新 破解版,结合 打造"帅"的编辑器 For .Net

    CKEditor4.1+CKFinder2.3.1 FOR MVC4 最新破解版:(2013-3-23) baidu share: http://pan.baidu.com/share/link?sh ...

  3. SQL语句调优 - 索引上的数据检索方法

    如果一张表上没有聚集索引,数据将会随机的顺序存放在表里.以dbo.SalesOrderDetail_TEST为例子.它的上面没有聚集索引,只有一个在SalesOrderID上的非聚集索引.所以表格的每 ...

  4. solr集成mmseg4j分词

    solr集成mmseg4j分词 mmseg4j https://code.google.com/p/mmseg4j/ https://github.com/chenlb/mmseg4j-solr 作者 ...

  5. SAP ECC CO 配置

    SAP ECC 6.0 Configuration Document Controlling (CO) Table of Content TOC \o \h \z 1. Enterprise Stru ...

  6. 构建基于WinRT的WP8.1 App 01:页面导航及页面缓存模式

    本篇博文主要阐述基于Windows Runtime的Windows Phone 应用页面间导航相关知识,主要分为以下几个方面: Window.Frame和Page概览 页面间实现跳转 处理物理后退键 ...

  7. nginx lua获取客户端ip

    --获取客户端ip function get_client_ip() local headers=ngx.req.get_headers() local ip=headers["X-REAL ...

  8. android自定义RadioGroup实现可以添加多种布局

    android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到r ...

  9. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  10. Qt 判断文件是否存在

    包含头文件:<QFileInfo> 代码: QFileInfo file("文件路径"); if(file.exists()==false){文件不存在:}