[LeetCode]Palindrome Partitioning 找出所有可能的组合回文
给定一个字符串,切割字符串,这样每个子字符串是一个回文字符串。
要找出所有可能的组合。
办法:暴力搜索+回溯
class Solution {
public:
int *b,n;
vector<vector<string> >ans;
void dfs(int id,string &s,int len){
if(id>=n){
if(len>0){
vector<string>vt;
vt.push_back(s.substr(0,b[0]+1));
for(int i=1;i<len;++i){
vt.push_back(s.substr(b[i-1]+1,b[i]-b[i-1]));
}
ans.push_back(vt);
}
return;
}
int j,k;
b[len]=id;
dfs(id+1,s,len+1);
for(j=id+1;j<n;++j){
for(k=0;id+k<j-k&&s[id+k]==s[j-k];++k);
if(id+k>=j-k){
b[len]=j;
dfs(j+1,s,len+1);
}
}
}
vector<vector<string> > partition(string s) {
n=s.size();
if(n==0)return ans;
if(n==1){
vector<string>vt;
vt.push_back(s);
ans.push_back(vt);
return ans;
}
b=new int[n];
dfs(0,s,0);
return ans;
}
};
版权声明:本文博主原创文章,博客,未经同意不得转载。
[LeetCode]Palindrome Partitioning 找出所有可能的组合回文的更多相关文章
- [LeetCode]Palindrome Number 推断二进制和十进制是否为回文
class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- shell脚本,检查给出的字符串是否为回文
[root@localhost wyb]# .sh #!/bin/bash #检查给出的字符串是否为回文 read -p "Please input a String:" numb ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- LeetCode: Palindrome Partitioning [131]
[称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- [leetcode]Palindrome Partitioning II @ Python
原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s ...
随机推荐
- osc搜索引擎框架search-framework,TngouDB,gso,
项目目的:OSChina 实现全文搜索的简单封装框架 License: Public Domain 包含内容: 重建索引工具 -> IndexRebuilder.java 增量构建索引工具 -& ...
- nginx源代码分析--高性能server开发 常见的流程模型
1.高性能server 对于高性能server对于.处理速度和占用空间小是典型特性.特别是当server经验C10K问题的时候(网络server在处理数以万计的client连接时.往往出现效率低下甚至 ...
- hdu 1240 Asteroids!(BFS)
题目链接:点击链接 简单BFS,和二维的做法相同(需注意坐标) 题目大意:三维的空间里,给出起点和终点,“O”表示能走,“X”表示不能走,计算最少的步数 #include <iostream&g ...
- 使用KnockoutJs+Bootstrap实现分页
[后端人员耍前端系列]KnockoutJs篇:使用KnockoutJs+Bootstrap实现分页 一.引言 由于最近公司的系统需要改版,改版的新系统我打算使用KnockoutJs来制作Web前端 ...
- C# openfiledialog设置filter属性后达不到过滤效果的原因之一
此处用RichTextBox控件举例>>> 在窗体对应的类中处理Load事件可以为openfiledialog设置Filter的属性: private void Form1_Load ...
- TRIZ系列-创新原理-29-气动或液压结构原理
气动或液压结构原理的详细表述例如以下:1)用气态或液态部件替代固体部件.能够用空气或者水,也能够用气垫或水垫,使这些部件膨胀.这条原理符合系统的动态性进化法则-柔性化.在改造系统时,我们能够尝试将系统 ...
- Storm构建分布式实时处理应用初探(转)
最近利用闲暇时间,又重新研读了一下Storm.认真对比了一下Hadoop,前者更擅长的是,实时流式数据处理,后者更擅长的是基于HDFS,通过MapReduce方式的离线数据分析计算.对于Hadoop, ...
- [WebGL入门]十,矩阵计算和外部库
注:文章译自http://wgld.org/,原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:],另外,鄙人webgl研究还不够深入,一些专业词语,假设翻译有误,欢迎大家指 ...
- unix解释器文件详解
exec执行普通文件和解释器文件的区别 2014-11-15 23:52:45 分类: LINUX exec执行普通文件和解释器文件的区别 ——lvyilong316 1. 从一个问题开始 首先要从项 ...
- HTC one/M7电信802d 毒蛇ViperOne2.1.0/高级毒蛇工具/完美root,精简/更多自定义,稳定,流畅ROM
ROM版本 HTC One/M7 802d ROM作者 雪狼团队·大盛 http://weibo.com/DaShengdd Android版本 Android 4.2.2 创建日期 2013.09. ...