题目:

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. PHP操作MongoDB学习(转)

    1  mongodb启动时,设置启动项 C:\>mongodb\bin\mongod --config C:\mongodb.conf 其中mongodb.conf为:    dbpath = ...

  2. Linux监控工具 (Linux Monitor Tools)

    最近发现几个好用的工具,顺便总结下. Zabbix和Nagios属于重量级,企业级Service procps-ng: top, free, ps, pgrep, vmstat ... sysstat ...

  3. 百度地图API 关键字模糊搜索

    http://api.map.baidu.com/place/v2/search?q=广场&region=汕头&output=json&ak=5E56A48675a5cd09a ...

  4. JAVA笔记 之 Thread线程

    线程是一个程序的多个执行路径,执行调度的单位,依托于进程存在. 线程不仅可以共享进程的内存,而且还拥有一个属于自己的内存空间,这段内存空间也叫做线程栈,是在建立线程时由系统分配的,主要用来保存线程内部 ...

  5. oracle rac理解和用途扩展

    Oracle RAC的优势在于利用多个节点(数据库实例)组成一个数据库,这样在保证了数据库高可用性的情况下更充分的利用了多个主机的性能,而且可以通过增加节点进行性能的扩展.实现Oracle RAC需要 ...

  6. Django:Model的Filter

    转自:http://www.douban.com/note/301166150/   django model filter 条件过滤,及多表连接查询.反向查询,某字段的distinct   1.多表 ...

  7. Python Django 开发 3 数据库CURD

    上一篇表建好后开始对数据进行CURD操作 dos输入: >>>python manage.py shell 以下的命令都是在shell中测试 (C)增: >>>im ...

  8. 配置EF链接 MySql 的方法

    材料: 1.MySQL for Visual Studio 1.2.4.msi 下载:http://dev.mysql.com/downloads/windows/visualstudio/ 2.my ...

  9. 移动安全时代,如何保护你的app

    Android系统的安全性历来备受诟病,在强大的反编译工具下,APK中的代码逻辑一览无余:重打包技术使得各种盗版软件层出不穷,充斥着Android市场,特别是对于金融.电商.游戏等产品的盗版应用,严重 ...

  10. Log4Net 手册

    首先感慨下,现在的程序员做的工作因为高级语言的生产力,系统框架模式的成熟,开源大牛的贡献,已经成越来越偏向 “面向配置编程”了...... 详细使用指南见文章:http://blog.csdn.net ...