class Solution {
public:
int minCut(string s) {
int len = s.length();
int* p_dp = new int[len + ];
char* s_dp = new char[len * len];
int* mm = new int[len + ];
mm[] = ;
memset(s_dp, -, len * len);
memset(p_dp, , (len + ) * sizeof(int)); int ret;
for (int i=; i<=len; i++) {
int sub_len = i;
int minc = INT_MAX; for (int j=; j<=i-; j++, sub_len--) { char* p_is = &s_dp[j * len + i-];
char b = ;
if (sub_len >= && - != (b = s_dp[(j+) * len + i - ])) {
*p_is = b && (s[j] == s[j + sub_len - ]);
}
if (*p_is == -) {
int p = j, q = j + sub_len - ;
for (; p < q && s[p] == s[q]; p++, q--);
*p_is = (p < q) ? : ;
}
if (*p_is == ) continue;
if (p_dp[j] < minc) minc = p_dp[j];
if (minc == mm[j]) break;
}
p_dp [i] = minc + ;
mm[i] = p_dp[i];
for (int k = i-; k >= && mm[k]>mm[k+]; k--) {
mm[k] = mm[k+];
}
}
ret = p_dp[len];
delete[] mm;
delete[] p_dp;
delete[] s_dp;
return ret - ;
}
};

原来想着既然前一题中已经想到了类似dp的方法,这一题应该更简单才是,不过。。。不过。。。没有对判断回文着过过程进行优化,一直TLE,把它考虑掉后就可以过了,这里把求字符串是否为回文的过程和求最小分割的过程合并了,并且考虑在不可能有更小分割的情况下快速进入下一个循环过程,悲剧的一天。

Leet 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. 欢迎使用CSDN-markdown编辑器u

    这里写自定义目录标题 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 创建一 ...

  2. WWDC: Thread Sanitizer and Static Analysis

    Thread Sanitizer 过程 编译过程中链接了一个新的库.  也可以通过命令行来操作: $ clang -fsanitize=thread source.c -o executable $ ...

  3. centos7启动顺序加密的问题

    在上一篇博客中我们说的是如何进入单用户模式,这篇我主要来讲centos7启动加密. 首先我们来说centos的启动顺序: 上一篇我们所说的进入单用户模式,就是在boot loader 这一层次下进入的 ...

  4. 【bzoj4240】 有趣的家庭菜园 树状数组

    这一题最终要构造的序列显然是一个单峰序列 首先有一个结论:一个序列通过交换相邻的元素,进行排序,最少的交换次数为该序列的逆序对个数 (该结论很久之前打表意外发现的,没想到用上了.....) 考虑如何构 ...

  5. 添加或删除 HTML dom元素

    添加或删除 HTML dom元素 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素追加该元素. <div id=" ...

  6. 剑指offer三十八之二叉树的深度

    一.题目 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 二.思路 递归,详见代码. 三.代码 public class So ...

  7. commons-pool2

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7375611.html 1.前言 本章介绍一下常用基础Jar包commons-pools2,最近使用到了thrif ...

  8. Form表单如何可以传递多个值传递List数组对象到后台的解决办法

    举例说明: 后台有一个对象 User ,结构如下: 后台有一个对象 User ,结构如下: public class User{ private String username; private Li ...

  9. 各种”xxx“ native gem required installed build tools 报错

    报错情况:(类似毛病 提示native gem require installed builld tools的解决方法是一样的)) 解决方法:http://rubyinstaller.org/down ...

  10. Linux的MySQL不能远程访问

    1.首先,你要确认用户是否只允许localhost访问: 在linux下登录mysql mysql -uroot -p密码;     use mysql;     select `host`,`use ...