[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 ...
随机推荐
- Apache+Django+Mysql环境配置
环境要求:Apache:2.2 Mysql:5.5 Django:1.5 python:2.7 首先下载mod_wsgi-win32-ap22py27-3.3.so 下载下来后,改名成mod_wsg ...
- Android开发四大组件概述
这个文章主要是讲Android开发的四大组件,本文主要分为 一.Activity具体解释 二.Service具体解释 三.Broadcast Receiver具体解释 四.Content Provid ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- HDU 2255 奔小康,赚钱(KM算法模板)
解决问题的思路: 二部图,正确的匹配,卡费用流,使用KM算法. #include <cstring> #include <algorithm> #include <cst ...
- Android ----制作自己的Vendor
Android源代码使用一个可定制的编译系统来生成 特定的,针对自己硬件平台的Android系统,比方不使用缺省的out/target/prodect/generic文件夹, 本文档简介了这个编译系统 ...
- 【Android小应用】颈椎保健操Android开源项目
前段时间在知乎上回答已入 IT 行业的前辈,有哪些关于保护身体健康的知识分享给 IT 新人? ,回复了一张图片,评论里面有知友希望通过程序可以实现,我后面尝试着通过程序实现了效果,现开源出来,大家可以 ...
- xcode6 iOS sdk8.1隐藏系统状态栏
在代码项目(uzplayer)从iOS6升级到iOS8之后,头发如今视频播放器有.系统状态栏后面的背景: 这样就会导致有的时候按下Donebutton,或者拖滑块没有效果 所以,我们须要想个办法.把这 ...
- Linux下搭建tomcat集群全记录(转)
本文将讲述如何在Linux下搭建tomcat集群,以及搭建过程中可能的遇到的问题和解决方法.为简单起见,本文演示搭建的集群只有两个tomact节点外加一个apache组成,三者将安装在同一机器上:ap ...
- poj3764(dfs+Trie树+贪心)
题目链接:http://poj.org/problem?id=3764 分析:好题!武森09年的论文中有道题CowXor,求的是线性结构上的,连续序列的异或最大值,用的办法是先预处理出前n项的异或值, ...
- python实现了字符串的按位异或和php中的strpad函数
近期在写自己主动化測试,因为开发加密中用到strpad和字符串的按位异或,而python中没有这种函数和功能,所以必须自己写一套,要不自己主动化測试无法进行,所以就用python实现了一下,因为在写字 ...