【Palindrome Partitioning】cpp
题目:
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的更多相关文章
- 【Palindrome Number】cpp
题目: Determine whether an integer is a palindrome. Do this without extra space. click to show spoiler ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Scramble String】cpp
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
随机推荐
- Python高效开发实战——Django、Tornado、Flask、Twisted
今天要推荐的就是这本书,内容涉及四种主流的Python Web开发框架,零基础完成网站搭建.数据库设计.前后端开发,全方位领悟Python原理与应用. 最新最全的框架实战,尽在这本书,可搜索亚马逊.京 ...
- 更改placeholder样式
/*不要将选择器进行组合*/ /* IE 10-11 */ :-ms-input-placeholder { color: #aaa; } /* webkit */ ::-webkit-input-p ...
- Python对Excel操作详解
Python对Excel操作详解 文档摘要: 本文档主要介绍如何通过python对office excel进行读写操作,使用了xlrd.xlwt和xlutils模块.另外还演示了如何通过Tcl ...
- JQuery笔录
1.jQuery 的 hide() 函数,隐藏了 HTML 文档中所有的 <p> 元素.<script type="text/javascript">$(d ...
- 扫描局域网ip的shell
# vim /mysh/ipscan.sh #!/bin/bash # scan the local alive ipaddress # -- if [ -f $filename ];then ech ...
- 【51nod1705】七星剑(成环DP)
点此看题面 大致题意: 你要把一把剑从0星升至7星,有n颗宝石供你选择,第i颗宝石的价值是c[i],用第i颗宝石将剑从k-1星升至k星的成功率是prob[k][i],而失败后会掉lose[k][i], ...
- py2exe --- show error: MSVCP90.dll + matplotlib issues
问题1: show error: MSVCP90.dll: No such file or directory 创建生成exe文件的脚本添加: import py2exe from distutils ...
- python_34_文件操作3
f=open('yesterday',encoding='utf-8') print(f.tell())#文件句柄所在指针指向的位置,即光标在哪里(按字符计数) f.readline()#读一行 pr ...
- codeforces 1114C
题目连接 : https://codeforces.com/contest/1114/problem/C 题目大意:给一个整数n(1e18>=n>=0),和一个整数k(1e12>=k ...
- 图片url转base64
var xhr = new XMLHttpRequest() // 配置的代理,解决跨域问题 xhr.open('GET', url.replace('http://xxx.com', '/img') ...