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. python操作mongodb实例

    安装pymongo扩展 import pymongo; client = pymongo.MongoClient(host='10.48.176.170',port=27017); db = clie ...

  2. StormUI详解

    StormUI由Cluster Summary,topology summary,supervisor summary,Nimbus Configuration四部分组成,如下图所示: Cluster ...

  3. C# 5.0-.Net新特性

    调用者信息特性 CallerMemberNameAttribute | CallerFilePathAttribute | CallerLineNumberAttribute .NET Framewo ...

  4. 钉钉h5项目实战|仿钉钉聊天|h5移动端钉钉案例

    最近一直着手开发h5仿钉钉项目,使用到了h5+css3+zepto+wcPop2等技术进行开发,实现了消息.表情.动图发送,仿QQ多人拼合图像,可以选择本地图片,并可以图片.视频预览,仿微信发红包及打 ...

  5. Collection、Set、List概念上的区别及关联

    类图如下:

  6. (转)mysql原生在线ddl和pt-osc原理解析

    原文:http://blog.csdn.net/zengxuewen2045/article/details/52017247 https://github.com/mysql-inception/i ...

  7. kafka 消费者offset记录位置和方式

    我们大家都知道,kafka消费者在会保存其消费的进度,也就是offset,存储的位置根据选用的kafka api不同而不同. 首先来说说消费者如果是根据javaapi来消费,也就是[kafka.jav ...

  8. 网络游戏程序员须知 UDP vs TCP(转)

    本文为作者原创或翻译,转载请注明,不得用于商业用途. 作者:rellikt@gmail.com 首发链接:http://blog.csdn.net/rellikt/archive/2010/08/21 ...

  9. Mysql 5.7版本安装:mysql 服务无法启动。

    一.解压文件 下载好MySQL后,解压到D盘下,也可以根据个人喜好解压在其他盘符的路径下,解压后的路径是:D:\mysql-5.7.17-winx64.解压好后不要太兴奋,需要配置默认文件呢! 二. ...

  10. javac之Inferring Type Arguments Based on Actual Arguments

    We use the following notational conventions in this section: Type expressions are represented using ...