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. MySQL(存储过程,支持事务操作)

    day61 保存在MySQL上的一个别名   >   一坨SQL语句 -- delimiter // -- create procedure p1() -- BEGIN -- select * ...

  2. Js验证15/18身份证

    var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古&quo ...

  3. 用switch函数根据选择不同的radio出现不同的视图

    html代码: <!DOCTYPE html> <html> <head> <title></title> <style type=& ...

  4. Oracle性能问题sql调优脚本集

    ---------------------------------------------------------------------------------------------------- ...

  5. 目标检测算法—YOLO-V1

    为什么会叫YOLO呢? YOLO:you only look once.只需要看一眼,就可以检测识别出目标,主要是突出这个算法 快 的特点.(原文:Yolo系列之前的文章:主要是rcnn系列的,他们的 ...

  6. 【xsy1144】选物品 主席树

    题目大意:$N$ 件物品摆成一排,给每个物品定义两个属性 $A$ 和$ B$,两件物品的 差异度 定义为它们两种属性的差的绝对值中较大的一个.如果要求出一些物品的差异度,我们先定义一个 理想物品,使它 ...

  7. 【xsy1147】 异或(xor) 可持久化trie

    我的脑回路可能比较奇怪. 我们对这些询问离线,将所得序列${a}$的后缀和建$n$棵可持久化$trie$. 对于一组询问$(l,r,x)$,我们在主席树上询问第$l$棵树$-$第r$+1$棵树中与$s ...

  8. OAuth 2.0 - Authorization Code授权方式详解

    I:OAuth 2.0 开发前期准备 天上不会自然掉馅饼让你轻松地去访问到人家资源服务器里面的用户数据资源,所以你需要做的前期开发准备工作就是把AppKey, AppSecret取到手 新浪获取传送门 ...

  9. Java执行Shell脚本“No such file or directory” (win->Linux)异常的可能原因

    转自:http://blog.csdn.net/zlpdaisy/article/details/6134314 用Runtime.getRuntime().exec()方法执行Linux的一个She ...

  10. Java之IO(十二)CharArrayReader和CharArrayWriter

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7082668.html 1.前言 本章介绍字符数组流,作用和ByteArrayInputStream字节数组流相同 ...