Palindrome Partitioning II
这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数。
Example:
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
状态:这个题的状态也非常好理解,dp[i]表示将s[0..i]分割成回文子串的最小分割次数。然后不急于寻找状态转移方程,我们先要明确如何用代码判断一个字符串的某个部分是不是回文,其实也很好理解啊,咱们可以分块来理解,毕竟这是个算法题,不可能用常规的那种遍历一半的方式来判断。首先对于这个字符串的某个子串s[j..i](j<=i),满足它是回文的条件两条(1)s[j]==s[i]  (2)  只确定两端的两个字符还不够,当这个子串的长度小于4或者去掉两端的相同字符后还是回文即可。现在我们除了递推数组dp,再定义一个记录数组pa[j][i],如果s[j..i]是回文,则pa[j][i] = 1,否则为0。有了这两个条件,我们就可以总结状态转移方程了:
dp[i] = min(dp[j-1]+1),当0<j<=i时,并且s[j..i]是回文时
dp[i] = 0 ,当j=0,并且s[j..i]是回文时
在具体的dp数组递推运算过程中,需要这两个分支,同时会更新pa[j][i]的值便于后面的过程中回文条件的判断。
 
 public int minCut(String s) {
int slen = s.length();
int[][]pa = new int[slen][slen];
int[]dp = new int[slen];
for(int i = 0;i<slen;i++)
dp[i] = i;
for(int i = 0;i<slen;i++)
Arrays.fill(pa[i],0);
for(int i = 1;i<slen;i++) {
for(int j = 0;j<=i;j++) {
if(s.charAt(j)==s.charAt(i)&&((i-j<2)||pa[j+1][i-1]==1)) {
pa[j][i] = 1;
if(j!=0)dp[i] = Math.min(dp[i],dp[j-1]+1);
else dp[i] = 0;
}
}
}
return dp[slen-1];
}

动态规划——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】132. Palindrome Partitioning II

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

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

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

  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 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. leetcode132. Palindrome Partitioning II

    leetcode132. Palindrome Partitioning II 题意: 给定一个字符串s,分区使分区的每个子字符串都是回文. 返回对于s的回文分割所需的最小削减. 例如,给定s =&q ...

随机推荐

  1. showdoc 文档管理系统

    ==========================showdoc 简介==========================在线文档管理系统很多, 比如阿里的语雀.腾讯的 TAPD 平台也包括文档管理 ...

  2. [置顶]Python开发之路

    阅读目录   第一篇:python入门 第二篇:数据类型.字符编码.文件处理 第三篇:函数 第四篇:模块与包 第五篇:常用模块 第六篇:面向对象 第七篇:面向对象高级 第八篇:异常处理 第九篇:网络编 ...

  3. 第30月第18天 autolayout代码

    1.上下左右 [tipsLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; { id view1 = tipsLabel; id view2 ...

  4. Centos环境下安装mongoDB

    安装前注意: 此教程是通过yum安装的.仅限64位centos系统 安装步骤: 1.创建仓库文件: vi /etc/yum.repos.d/mongodb-org-3.4.repo 然后复制下面配置, ...

  5. go [第一篇]初识

    [第一篇] 简介 Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发 ...

  6. (五)ORBSLAM关键帧的筛选和插入

    ORBSLAM2的关键帧简介 图像插入频率过高会导致信息冗余度快速增加,而这些冗余的信息对系统的精度却十分有限,甚至没有提高,反而消耗了更多的计算资源.这等于吃力不讨好. 关键帧的目的在于,适当地降低 ...

  7. 通用RSA加密 - PHP+Java+Javascript加密解密

    php端生成 公钥私钥 1.openssl genrsa -out rsa_private_key.pem 1024    私钥 2.openssl rsa -in rsa_private_key.p ...

  8. [转] GloVe公式推导

    from: https://pengfoo.com/post/machine-learning/2017-04-11 GloVe(Global Vectors for Word Representat ...

  9. mac下Android开发环境的配置

    近似一天的时间,终于把Android环境配置好了. 总结:主要问题在于android的网站是国外,下载东西的时候需要vpn才可以.所以会出现各种各样的问题. 环境:Android Studio + S ...

  10. 初识中间件Kafka

    初识中间件Kafka Author:SimplelWu 什么是消息中间件? 非底层操作系统软件,非业务应用软件,不是直接给最终用户使用的,不能直接给客户带来价值的软件统称为中间件 关注于数据的发送和接 ...