leetcode132. Palindrome Partitioning II

题意:

给定一个字符串s,分区使分区的每个子字符串都是回文。

返回对于s的回文分割所需的最小削减。

例如,给定s =“aab”,

返回1,因为可以使用1切割生成回文分割[“aa”,“b”]。

思路:

一开始我用dfs + 贪心来做,找到最长的回文,然后拆掉继续dfs,然后最后一个测试样例过不了,不是复杂度的问题,是算法本身有问题。

看了别人的思路,是用dp来做的,设DP[i][j]表示第i个字符到第j个字符是否构成回文,若是,则DP[i][j]=1;若否,则DP[i][j]=0;如此,根据回文的约束条件(对称性),DP[i][j]构成回文需满足:

1、输入字符串s[i]==s[j],对称性;

2、条件1满足并不能保证i到j构成回文,还须:(j-i)<=1或者DP[i+1][j-1]=1;即,i、j相邻或者i=j,也就是相邻字符相等构成回文或者字符自身构成回文,如果i、j不相邻或者相等,i到j构成回文的前提就是DP[i+1][j-1]=true

所以状态转移方程:DP[i][j]=(s[i]s[j]&&(j-i<=1||DP[i+1][j-1]1))。由于i必须小于j,i>=0&&i<s.length().如此,DP[i][j]构成一个下三角矩阵,空间、时间复杂度均为O(n2),如下所示:对于长度为4的字符串s=“baab”,其中红色部分为i>j,为无意义部分;绿色部分i==j,即字符串自身必然构成回文,DP[i][j]=1;白色部分,为长度大于1的子串,需要状态转移方程进行判断。

ac代码:

C++

class Solution {
public:
int minCut(string s) {
int len = s.length();
vector<vector<bool> >map(len,vector<bool>(len));
for(int i = len - 1; i >= 0; i--)
{
for(int j = i; j < len; j++)
{
if(i == j)
{
map[i][j] = true;
}
else if(i + 1 == j)
{
if(s[i] == s[j])
{
map[i][j] = true;
}
}
else
{
if(s[i] == s[j] && map[i + 1][j - 1])
{
map[i][j] = true;
}
}
}
} vector<int> res(len + 1,0);
for(int i = len - 1; i >= 0; i--)
{
res[i] = res[i + 1] + 1;
for(int j = i + 1; j < len; j++)
{
if(map[i][j] == true && res[j + 1] + 1 < res[i])
res[i] = res[j + 1] + 1;
}
}
return res[0] - 1;
}
};

python


参考博文

leetcode132. Palindrome Partitioning II的更多相关文章

  1. 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)

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

  2. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

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

  3. 【leetcode】Palindrome Partitioning II

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

  4. leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...

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

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

  6. 动态规划——Palindrome Partitioning II

    Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...

  7. leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II

    131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...

  8. LeetCode: Palindrome Partitioning II 解题报告

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

  9. 【LeetCode】132. Palindrome Partitioning II

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

随机推荐

  1. PHP中的 get_magic_quotes_runtime

    get_magic_quotes_runtime() 获得外部文件及数据库资料时是否进行转义 set_magic_quotes_runtime(1); 临时设置获得外部文件及数据库资料时是否进行转义 ...

  2. elasticsearch集群介绍及优化【转】

    elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展)或者购买更多的服务器(横向扩展),Elasticsearch能从更强大的硬件中获得更好的性能,但是纵 ...

  3. 34.Find First and Last Position of Element in Sorted Array---头条面试题、《剑指offer》38

    题目链接 题目大意:找出一串升序数组中target值的起始下标和结束下标值,如果不存在则返回{-1,-1}. 解法一:用二分查找,找到数组中的target,然后找其左边和右边的target下标值.代码 ...

  4. nginx allow 多个ip & ipv4的网段表示方法解析

    参考:https://www.baidu.com/link?url=5aVe_syihQzhHnSDAdLsNNQYqDe_W2GYG1GeIQ130e4mEZbusxQfqGVTdg-dJg8fqM ...

  5. 集合遍历过程iterator, 添加删除元素报异常

    list  set  遍历过程中添加或者删除元素,报异常. 使用iterator 也会报异常 ConcurrentModificationException remove只能用迭代器的remove,而 ...

  6. Selenium_Grid

    Selenium Grid 工作原理 Grid是一种分布式测试工具,整个结果由一个hub主节点和若干个node代理节点组成. hub用来管理各个代理节点的注册和状态信息,并且接收远程客户端代码请求调用 ...

  7. 深度学习方法(九):自然语言处理中的Attention Model注意力模型

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.NET/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 上一篇博文深度学习方法(八):Enc ...

  8. 洛谷P2827 蚯蚓 题解

    洛谷P2827 蚯蚓 题解 题目描述 本题中,我们将用符号 ⌊c⌋ 表示对 c 向下取整. 蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. 蛐蛐国里现 ...

  9. WordPress用户角色与用户能力/权限

    WordPress用户角色(user roles)是WP或者其它插件增加的,可以让网站管理员(网站管理员也是一种角色)来方便的管理用户的权限/能力(Capabilities,一般情况下,一种角色不止有 ...

  10. sharepoint2013搜索

    参考http://www.cnblogs.com/jianyus/p/3272692.html 最小权限http://www.cnblogs.com/awpatp/archive/2011/08/16 ...