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

要找出所有可能的组合。

办法:暴力搜索+回溯

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. 旧发票要保留SIRET等信息,或者整个PDF

    查看旧发票时,每次都实时生成发票是不行的,因为公司的SIRET居然会是变的!!

  2. SLIC superpixel实现分析

    http://infoscience.epfl.ch/record/149300这是SLIC算法的官网,网站有和SLIC相关的资源. SLIC主要运用K-means聚类算法进行超像素的处理,聚类算法中 ...

  3. PyMOTW: heapq¶

    PyMOTW: heapq — PyMOTW Document v1.6 documentation PyMOTW: heapq¶ 模块: heapq 目的: 就地堆排序算法 python版本:New ...

  4. Java时间比較

    Date类有两个方法 一个是after()比方date1.after(date2)推断date1是否在date2之后也就是说date1小于date2吧, 一个是before()比方date1.befo ...

  5. VSTO学习笔记(一)VSTO概述

    原文:VSTO学习笔记(一)VSTO概述 接触VSTO纯属偶然,前段时间因为忙于一个项目,在客户端Excel中制作一个插件,从远程服务器端(SharePoint Excel Services)上下载E ...

  6. 乞讨N!到底有多少0

    分析: 对N质因数分解 N=2^x * 3^y * 5^z....因为10 = 2*5,所以末尾0的个数仅仅和x与z有关,每一对2和5相乘能够得到一个10.于是末尾0的个数=min(x,z).在实际中 ...

  7. SQL_由创建表引出

    ***********************************************声明*************************************************** ...

  8. 使用POI生成Excel报表

    先把报表模板截图贴上来 下面是POI编写的报表生成类ExcelReport.java package com.jadyer.report; import java.io.FileNotFoundExc ...

  9. 基于Hadoop的地震数据分析统计

    源码下载地址:http://download.csdn.net/detail/huhui_bj/5645641 opencsv下载地址:http://download.csdn.net/detail/ ...

  10. 关于静态与动态编译arm平台程序的比較

    因为近期弄个console程序,调用了readline,ncurses库,这两个动态库加起来有四百多k.而程序事实上非常小,其它地方也没使用到这两个库 所以想静态编译看看console程序有多大. # ...