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. ASP.Net获取Aras连接,并获取Innovator实例

    首先需要在自己的项目bin目录下引入Aras的dll(../Aras\Innovator\Innovator\Server\bin). 注意:在引入Aras的dll时.需要注意自己的操作系统的位数.因 ...

  2. lillietest 正态分布的拟合优度测试

    函数 lillietest格式 H = lillietest(X) %对输入向量X进行Lilliefors测试,显著性水平为0.05.H = lillietest(X,alpha) %在水平alpha ...

  3. .net 解压缩 rar文件

    public static class RARHelper { public static bool ExistsWinRar() { bool result = false; string key ...

  4. Microsoft Internet Explorer v11 / XML External EntityInjection 0day

    [+] Credits: John Page (aka hyp3rlinx) [+] Website: hyp3rlinx.altervista.org[+] Source:  http://hyp3 ...

  5. DES加密ECB(模式) golang

    Java默认DES算法使用DES/ECB/PKCS5Padding,而golang认为这种方式是不安全的,所以故意没有提供这种加密方式,那如果我们还是要用到怎么办?下面贴上golang版的DES EC ...

  6. 自适应rem.js

    用rem.js来实现自适应屏幕大小,要注意border不用rem做单位  代码如下: (function (doc, win) { //orientationchange : 判断手机是水平方向还是垂 ...

  7. Spring缓存注解@Cacheable

    @Cacheable @Cacheable 的作用 主要针对方法配置,能够根据方法的请求参数对其结果进行缓存 @Cacheable 作用和配置方法 参数 解释 example value 缓存的名称, ...

  8. 关于ViewPager+Fragment中的坑

    1.我的情况是Activity里嵌套了Fragment_0,然后Fragment_0里面又嵌套了两个Fragment:Fragment_1.Fragment_2,然后我在其中一个Fragment,Fr ...

  9. day17——其他内置函数

    zip函数: print(list(zip(('a','b','c'),(1,2,3)))) p={'name':'alex','age':18,'gender':'none'} print(list ...

  10. Linux库多重依赖

    源文件: //world.cpp #include <stdio.h> void world(void) { printf("world.\n"); } //hello ...