给定一个字符串,切割字符串,这样每个子字符串是一个回文字符串。

要找出所有可能的组合。

办法:暴力搜索+回溯

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 找出所有可能的组合回文的更多相关文章

  1. [LeetCode]Palindrome Number 推断二进制和十进制是否为回文

    class Solution { public: bool isPalindrome2(int x) {//二进制 int num=1,len=1,t=x>>1; while(t){ nu ...

  2. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  3. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  4. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  5. shell脚本,检查给出的字符串是否为回文

    [root@localhost wyb]# .sh #!/bin/bash #检查给出的字符串是否为回文 read -p "Please input a String:" numb ...

  6. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  8. LeetCode: Palindrome Partitioning [131]

    [称号] Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  9. [leetcode]Palindrome Partitioning II @ Python

    原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/ 题意: Given a string s, partition s  ...

随机推荐

  1. gant

    http://gant.github.io/ http://gant.codehaus.org/

  2. DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类)

    DAO设计模式实现数据库的增删改查(进一步封装JDBC工具类) 一.DAO模式简介 DAO即Data Access Object,数据访问接口.数据访问:故名思义就是与数据库打交道.夹在业务逻辑与数据 ...

  3. hdu4521 小明系列的问题——小明序列(LIS变种 (段树+单点更新解决方案))

    链接: huangjing 题目:中文题目 思路: 1:这个题目假设去掉那个距离大于d的条件,那么必定是一个普通的LIS.可是加上那个条件后就变得复杂了.我用的线段树的解法. . .就是採用延迟更新的 ...

  4. String的split

    对于  http://10.13.30.22/svn/SVNRepository/UnChecked/Test  想要分割他就要用: String subContent[]=modelInfo.get ...

  5. phantomjs,selenium,pyv8,pythonwebkit,,,,,,,,,,,,,

    Pyv8,PythonWebKit,Selenium,PhantomJS,Ghost.py 等等.... 快速构建实时抓取集群[searchtb] 定义:http://i.cnblogs.com/Ed ...

  6. JsonCpp Documentation

    JsonCpp - JSON data format manipulation library JsonCpp Documentation 0.6.0-rc2 Introduction JSON (J ...

  7. Java调用cmd压缩文件

    今天在做一个java调用windows的压缩命令时遇到一奇怪问题代码如下: String cmd ="C:/Program Files (x86)/WinRAR/rar.exe a c:/t ...

  8. U10vim程序编辑器

    vim需要多加练习. 1.你可以将vim视为vi的高级版本.vi分成三种模式:一般模式,编辑模式和命令行模式. 一般模式:以vi打开一个文件就直接进入一般模式了(这也是默认的模式).在这个模式中,你可 ...

  9. Android 一些错误

    android fragment里面放viewpager 嵌套fragment 报错: 解决:在adapter的构造方法里加上 super(fragment.getChildFragmentManag ...

  10. Linux 文件系统(二)---运行过程及结构间的关系

    (内核2.4.37) 一.首先.看看磁盘.超级块,inode节点在物理上总体的分布情况: (图示来自:www.daoluan.net) 对于一个分区,相应一个文件系统,一个文件系统事实上本质上还是磁盘 ...