Palindrome Partitioning

Total Accepted: 21056 Total
Submissions: 81036My Submissions

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"]
]

题意:切割字符串,使每一个子串都是回文

思路:dfs

选择一个切割点时,假设从起点到切割点的子串是回文,那么该切割点是合法的。能够选择

按这个规则dfs枚举就能够了

复杂度:时间O(n)。空间O(log n)

vector<vector<string> >res;

bool is_palindrome(const string &s, int start, int end){
while(start < end){
if(s[start] != s[end - 1]) return false;
++start; --end;
}
return true;
} void dfs(const string &s, int cur, vector<string>& partitions){
int size = s.size();
if(cur == size){
res.push_back(partitions);
}
for(int end = cur + 1; end <= size; ++end){
if(is_palindrome(s, cur, end)){
partitions.push_back(s.substr(cur, end - cur));
dfs(s, end, partitions);
partitions.pop_back();
}
}
} vector<vector<string>> partition(string s) {
vector<string> tem;
dfs(s, 0, tem);
return res;
}

最新文章

  1. IE7 浏览器下面设置text-indent属性变成margin属性BUG
  2. [ZZ] A Proposal For Compiling Direct3D HLSL With LLVM (Written by Michael Larabel )
  3. 运维利器-ClusterShell集群管理操作记录
  4. javascrit2.0完全参考手册(第二版) 第2章第4节 基本的数据类型
  5. JSOI 2008 火星人prefix
  6. ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
  7. Inside Kolla - 01 简介
  8. Win7 VMWare 串口通信
  9. 基于Struts2框架实现登录案例 之 使用Struts2标签库简化表单+继承ActionSupport完成输入交验
  10. 恼人的Visual Studio 2010崩溃重启问题
  11. IO 流—&gt;&gt;&gt;补充
  12. IC芯片設計
  13. [国嵌笔记][019][Eclipse集成开发环境]
  14. Gradle 的Daemon配置
  15. HTMLConverter使用实例(转)
  16. 包管理工具之Pipenv
  17. Web高级 Ajax和跨域CORS
  18. MySQL数据库对象-索引
  19. Android百大框架排行榜
  20. Telnet远程登录

热门文章

  1. hdoj--3339--In Action(最短路+01背包)
  2. 09.ws复杂数据类型数据传输
  3. 一起学Android之Fragment
  4. python print 显示不同的字体
  5. 几道leetcode不会做的题目
  6. Android图片剪裁库
  7. java RPC系列之二 HTTPINVOKER
  8. in 与 exist , not in 与 not exist
  9. Linker scripts之SECTIONS
  10. UML+模式设计概述