Palindrome Partitioning

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

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

  [
["aa","b"],
["a","a","b"]
]
为了加速运算,可以利用动态规划,求出满足回文的子串的位置
 
palindrome[i][j]表示了第字符串中,s[i,i+1,……, j]是否是回文
 
可以有以下递推公式:
if(i==j) palindrome[i][j]=true;
if(i-j=1)palindrome[i][j]=s[i]==s[j];
if(i-j>1)palindrome[i][j]=palindrome[i+1][j-1]&&s[i]==s[j]
 
 
得到了该回文表后,我们利用回溯法得到所有的子串
 
 
 
 class Solution {
public: vector<vector <string> > res; vector<vector<bool> > palindrome;
string s;
int n; vector<vector<string>> partition(string s) { this->s=s;
this->n=s.length(); vector<vector<bool> > palindrome(n,vector<bool>(n));
getPalindrome(palindrome);
this->palindrome=palindrome; vector <string> tmp;
getPartition(,tmp); return res;
} //回溯得到子串
void getPartition(int start,vector<string> tmp)
{ if(start==n)
{
res.push_back(tmp);
return;
} for(int i=start;i<n;i++)
{
if(palindrome[start][i])
{
tmp.push_back(s.substr(start,i-start+));
getPartition(i+,tmp);
tmp.pop_back();
}
}
} void getPalindrome(vector<vector<bool> > &palindrome)
{
int startIndex=;
int endIndex=n-; for(int i=n-;i>=;i--)
{
for(int j=i;j<n;j++)
{
if(i==j)
{
palindrome[i][j]=true;
}
else if(j-i==)
{
palindrome[i][j]=(s[i]==s[j]);
}
else if(j-i>)
{
palindrome[i][j]=(s[i]==s[j]&&palindrome[i+][j-]);
}
}
}
}
};

【leetcode】Palindrome Partitioning的更多相关文章

  1. 【LeetCode】Palindrome Partitioning 解题报告

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

  2. 【leetcode】Palindrome Partitioning II(hard) ☆

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

  3. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  4. 【Leetcode】【Medium】Palindrome Partitioning

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

  5. 【leetcode】 Palindrome Partitioniong (middle) (*^__^*)

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

  6. 【leetcode】Palindrome Number

    题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...

  7. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  8. 【leetcode】Palindrome Number (easy)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  9. 【LeetCode】Palindrome Number(回文数)

    这道题是LeetCode里的第9道题. 题目说的: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: ...

随机推荐

  1. 用form表单实现Ajax---post提交

    实例讲解:新闻发布实现无刷新上传,显示 html代码: 注意:文本框中并没有id  ,,只有name.jquery获取每个文本框的值还要在拼写提交格式(id=value&name=value& ...

  2. Linux安装卸载JDK(1.7 rpm)

    一.卸载系统已有的JDK 1.查看已安装的jdk rpm -qa|grep jdk   2.卸载jdk rpm -e --nodeps java-1.6.0-openjdk-1.6.0.0-1.66. ...

  3. 从零开始设计SOA框架(二):请求/响应参数的设计

    每个接口都有请求参数.响应参数.其中请求参数分为公共参数和业务参数.响应参数分为两类:正常的响应参数.统一的错误参数   一.请求参数 1.公共参数:每个接口都有的参数,主要包含appkey.时间戳. ...

  4. codevs1227 方格取数2 注意数组啊啊啊啊啊啊啊啊啊啊

    一开始T了一组RE了一组,实在找不出错来,就把数组加了一个0竟然就多A了一组.很惊讶的又加了几个0最后竟然全A了!!! 懒得做了,改的是之前的那个蚯蚓的游戏问题.还是需要拆点,至于为什么不能重复走结点 ...

  5. BZOJ-1036 树的统计Count 链剖线段树(模板)=(树链剖分+线段树)

    潇爷昨天刚刚讲完...感觉得还可以...对着模板打了个模板...还是不喜欢用指针.... 1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Lim ...

  6. SQLServer错误:过程 sp_addextendedproperty,第 xxx 行对象无效。'dbo.xxx.xxx' 不允许有扩展属性,或对象不存在。

    上传数据库到虚拟主机,在执行SQL脚本的时候出现以下的错误: 消息 15135,级别 16,状态 8,过程 sp_addextendedproperty,第 37 行 对象无效.'dbo.Messag ...

  7. jmap命令详解

    1.命令基本概述 Jmap是一个可以输出所有内存中对象的工具,甚至可以将VM 中的heap,以二进制输出成文本.打印出某个java进程(使用pid)内存内的,所有‘对象’的情况(如:产生那些对象,及其 ...

  8. Memcache和Redis

  9. hdu 1205 吃糖果

    思路: 仔细想想,想要不重复吃一种糖果, 把所有糖果吃完,只要所有糖果的和,减去最多的糖果+1>=最多糖果的数量即可不重复吃完. #include <stdio.h> int mai ...

  10. php + mysql 分布式事务(转)

    事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元: 事务应该具有4个属性:原子性.一致性.隔离性.持续性 原子性(atomicity).一个事务是一个不可分割的工作单 ...