题目:

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. mysql数据库字段类型的选择原则

    原文链接:http://blog.csdn.net/u013412790/article/details/51615407 数据库类型的选择对数据库的性能影响很大 1 . 数据类型会影响存储空间的开销 ...

  2. Openfire+spark在linux上搭建内部聊天系统

    一.    实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...

  3. TFS无法确定工作区解决方案

    当TFS遇到无法确定工作区时,删除掉SUO和VSSSCC文件,当然也有可能不是这两个,反正是SLN文件以外的两个文件,毕竟是昨天做的事,有点忘记了.所以,做掉文件之前,要做好备份O(∩_∩)O~

  4. 汇编:jmp系列跳转指令总结

    助记方法: J:跳转C: 进位位置位N: 否S: 符号位置位o: 溢出位置位Z: 零标志位置位E: 等于P:奇偶位置位A: AboveB: BelowL: Less (Little的比较级)G: Gr ...

  5. getline()读入一整行

    string line; getline(cin, line); cin不能读入空行,用getline可以读入空行.

  6. IOS Quarzt2D 手动的释放

    - (void)drawRect:(CGRect)rect { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.绘制 ...

  7. bzoj2600 [Ioi2011]ricehub

    Description 乡间有一条笔直而长的路称为“米道”.沿着这条米道上 R 块稻田,每块稻田的坐标均为一个 1 到 L 之间(含 1 和 L)的整数.这些稻田按照坐标以不减的顺序给出,即对于 0 ...

  8. 2018.6.22 Java试题测试结果

    如何从有数字规律的网址抓取网页并保存在当前目录?假设网址为 http://test/0.xml,其中这个数字可以递增到100. for((i=0;i<100;++i));do wget http ...

  9. 感谢我的python老师

    Python自动化开发(金角大王版) http://www.cnblogs.com/alex3714/articles/5885096.html

  10. js数据结构处理--------树结构数据遍历

    1.深度遍历 深度遍历利用栈来实现 class Stack { constructor () { this.top = 0, // 栈的长度 this.list = [] } push(item) { ...