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

要找出所有可能的组合。

办法:暴力搜索+回溯

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. [Android学习笔记]Android Library Project的使用

    RT http://developer.android.com/tools/projects/index.html

  2. [置顶] iframe使用总结(实战)

    说在前面的话,iframe是可以做很多事情的. 例如: a>通过iframe实现跨域; b>使用iframe解决IE6下select遮挡不住的问题 c>通过iframe解决Ajax的 ...

  3. Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包

    Computational Network Toolkit (CNTK) 是微软出品的开源深度学习工具包 用 CNTK 搞深度学习 (一) 入门 Computational Network Toolk ...

  4. C++ 中获取 可变形參函数中的參数

    #include <iostream> #include <stdarg.h> using namespace std; int ArgFunc(const char * st ...

  5. cocoa动态方法决议及消息转发

    假设给一个对象发送不能响应的消息,同一时候又没有进行动态方法决议,又没实现消息转发,那么就会引发以下的crash信息 2014-07-30 15:47:54.434 MethodNotFind[171 ...

  6. sha1加密java代码

    sha1 加密 java代码 public static String getSha1(String str){ if(str==null||str.length()==0){ return null ...

  7. 本地或者服务器同时启动2个或多个tomcat

    一,修改配置文件server.xml的端口 C:\apache-tomcat-5.5.23-1\conf\server.xml用记事本什么的打开修改3个地方   第一: <Server port ...

  8. 13、Cocos2dx 3.0三,找一个小游戏开发3.0中间Director :郝梦主,一统江湖

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27706967 游戏中的基本元素 在曾经文章中,我们具 ...

  9. Java-UrlRewrite中文api文档

    安装 1. 下载jar包, 并加入到WEB-INF/lib下 2. 在WEB-INF/web.xml中增加下面的配置 <filter> <filter-name>UrlRewr ...

  10. 开源论坛jforum的集成

    Jforum是一款开源的java类的论坛,小巧高效,运用了很多JSP新技术,支持hsqldb.oracle.mysql. postgresql数据库,完全遵从MVC设计模式. 1.首先下载最新的版本( ...