【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 ...
随机推荐
- 零基础逆向工程32_Win32_06_通用控件_VM_NOTIFY
标准控件与可用控件 windows标准控件,标准控件总是可用的 Static Group Box Button Check Box Radio Button Edit ComboBox ListBox ...
- jeesit 部署404
1.刷新项目 2.clean 项目 3.重新部署项目 4.Ran as maven build 后在重新部署 5.重新导入maven项目
- centos6.5_64bit-nginx安装部署
1.配置防火墙,开启80端口.3306端口 vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dpor ...
- TP5.0:同一个控制器访问不同方法
首先,我把TP框架的内容放置在manualtp5文件夹 在manualtp5/application/index/controller/index控制器中定义两个方法: 我们都知道,如果我们网址中不输 ...
- pat乙级1034
1.vs2013不能用scanf,改为scanf_s,但是提交时不能用scanf_s,用scanf... scanf_s(], &a[], &b[], &b[]); 2.c++ ...
- Android(java)学习笔记74:ListViewProject案例(ListView + ArrayAdapter)
1. 首先是MainActivity.java文件,如下: package com.himi.lv1; import java.util.ArrayList; import java.util.Lis ...
- 【转】svn图标不显示的解决方案
最近发现svn图标莫名其妙的不显示,其他操作都正常.在网上搜了一些方法. 解决方法一(失败): 升级最新版本,我的本来就是最新版本 解决方法二(失败): 右键->TortoiseSVN-> ...
- 深入理解计算机系统_3e 第十章家庭作业 CS:APP3e chapter 10 homework
10.6 1.若成功打开"foo.txt": -->1.1若成功打开"baz.txt": 输出"4\n" -->1.2若未能成功 ...
- 自动生成 WebApi 在线说明文档。
1.使用Swashbuckle实现 Swashbuckle 是.NET类库,可以将WebAPI所有开放的控制器方法生成对应SwaggerUI的JSON配置.再通过SwaggerUI 显示出来.类库中已 ...
- 在maven项目中 配置代理对象远程调用crm
1 在maven项目中配置代理对象远程调用crm 1.1 在项目的pom.xml中引入CXF的依赖 <dependency> <groupId>org.apache.cxf&l ...