题目:

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:
vector<vector<string>> partition(string s) {
vector<vector<string> > ret;
vector<string> tmp;
Solution::dfs(ret, tmp, s, , s.size()-);
return ret;
}
static void dfs(vector<vector<string> >& ret, vector<string>& tmp, string& s, int begin, int end)
{
if ( begin>end ) { ret.push_back(tmp); return; }
for ( int i = begin; i <= end; ++i )
{
if ( Solution::isPalindrome(s, begin, i) )
{
tmp.push_back(s.substr(begin,i-begin+));
Solution::dfs(ret, tmp, s, i+, end);
tmp.pop_back();
}
}
}
static bool isPalindrome(string& s, int begin, int end)
{
while ( begin<end && s[begin]==s[end] ) { begin++; end--; }
return begin>=end;
}
};

tips:

把问题转化为深搜:字符串s有n个字符,因此可以有n个切的位置(包括不切)。

按照深搜的模板写出来代码(dfs终止条件;深搜遍历条件等)。

注意一个细节,传入下一层dfs时是从i+1到end,之前一直写成了begin+1犯了低级错误。

深搜的时间复杂度为O(2^n) 空间复杂度为O(1)。

深搜在这道题上也有不足之处,很多子字符串是否是回文算过了不止一遍,自然联想能否用动规算法保存每一轮的判断结果。

====================================================

先往后走,后面再研究动态规划的做法。

==================================================

第二次过这道题,用dfs做的,其中每层里面i代表的是截取字符串的长度,自然由1到s.size()。

class Solution {
public:
vector<vector<string> > partition(string s)
{
vector<vector<string> > ret;
vector<string> tmp;
Solution::dfs(ret, tmp, s);
return ret;
}
static void dfs(vector<vector<string> >& ret, vector<string>& tmp, string s)
{
if ( s.size()< )
{
ret.push_back(tmp);
return;
}
for ( int i=; i<=s.size(); ++i )
{
if ( Solution::isPalindrome(s.substr(,i)) )
{
tmp.push_back(s.substr(,i));
Solution::dfs(ret, tmp, s.substr(i,s.size()-i));
tmp.pop_back();
}
}
}
static bool isPalindrome(string s)
{
int begin = ;
int end = s.size()-;
while ( begin<end )
{
if ( s[begin]!=s[end] ) return false;
begin++;
end--;
}
return true;
}
};

【Palindrome Partitioning】cpp的更多相关文章

  1. 【Palindrome Number】cpp

    题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...

  2. 【palindrome partitioning II】cpp

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

  3. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  4. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  5. 【Scramble String】cpp

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  6. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  9. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

随机推荐

  1. Coursera 算法二 week 3 Baseball Elimination

    这周的作业不需要自己写算法,只需要调用库函数就行,但是有些难以理解,因此用了不少时间. import edu.princeton.cs.algs4.FlowEdge; import edu.princ ...

  2. linux 命令——4 mkdir (转)

    linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命令 ...

  3. 求和VII

    问题 K: 求和VII 时间限制: 2 Sec  内存限制: 256 MB提交: 422  解决: 53[提交] [状态] [讨论版] [命题人:admin] 题目描述 master对树上的求和非常感 ...

  4. 2018.6.4 Oracle数据库预定义的异常列表

    declare v_ename emp.ename%type; begin select ename into v_ename from emp where empno=&gno; dbms_ ...

  5. 2017.12.4 JavaWeb中EL表达式的运用

    <%@ page contentType="text/html; charset=gb2312"%> <html> <head> <tit ...

  6. Hive之数据模型

    (本文是基于多篇文章根据个人理解进行的整合,参考的文章见末尾的整理) 数据模型 hive的数据模型包括:database.table.partition和bucket. 1.Database:相当于关 ...

  7. AngularJs学习笔记-组件生命周期

    组件生命周期 (1)组件生命周期钩子 constructor:组件创建时被创建 ngOnChanges: 父组件修改或初始化子组件的输入属性时被调用,如果子组件没有输入属性,则永远不会被调用,它的首次 ...

  8. 用gulp把less文件编译成css文件

    第一次使用gulp构建工具,使用gulp将.less文件编译成.css文件并输出.根据视频做了笔记.提供新手和自己以后做参考. HTML文件 <!DOCTYPE html> <htm ...

  9. Vue中结合clipboard实现复制功能

    首先现在Vue中引入clipboard npm install clipboard --save 在需要使用的组件中import 引入clipboard import Clipboard from ' ...

  10. Mint UI文档

    Mint UI文档:http://elemefe.github.io/mint-ui/#/ 一.Mint UI的安装和基本用法. 1.NPM :npm i mint-ui -S 建议使用npm进行安装 ...