Leet Palindrome Partitioning II
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的更多相关文章
- 19. Palindrome Partitioning && Palindrome Partitioning II (回文分割)
Palindrome Partitioning Given a string s, partition s such that every substring of the partition is ...
- LeetCode:Palindrome Partitioning,Palindrome Partitioning II
LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 动态规划——Palindrome Partitioning II
Palindrome Partitioning II 这个题意思挺好理解,提供一个字符串s,将s分割成多个子串,这些字串都是回文,要求输出分割的最小次数. Example:Input: "a ...
- leetcode 131. Palindrome Partitioning 、132. Palindrome Partitioning II
131. Palindrome Partitioning substr使用的是坐标值,不使用.begin()..end()这种迭代器 使用dfs,类似于subsets的题,每次判断要不要加入这个数 s ...
- LeetCode: Palindrome Partitioning II 解题报告
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- 【LeetCode】132. Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
随机推荐
- Linux 操作日志
这里: /var/log/messages
- PHP中使用CURL模拟登录并获取数据实例
cURL 是一个功能强大的PHP库,使用PHP的cURL库可以简单和有效地抓取网页并采集内容,设置cookie完成模拟登录网页,curl提供了丰富的函数,开发者可以从PHP手册中获取更多关于cURL信 ...
- git常用的命令你知道有哪些?
1.git与svn的区别 1,git是目前世界上最先进的分布式版本控制系统,他没有中央服务器,每个人的电脑就是一个完整的版本库,这样,工作的时候不需要联网 2,svn是集中式版本控制系统,版本库是集中 ...
- Karatsuba乘法--实现大数相乘
Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...
- 编写Android程序Eclipse连不上手机。
主要问题有: 1.开发者选项没有开启 2.设备管理器中MTP有黄色小叹号 3.ADB异常. 问题1容易解决. 问题2,3困扰了我很长时间,网上的很多解决方法是下载安装MTP驱动,或者直接右击更新驱动. ...
- CentOS6.5下Ambari安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)
第一步: Ambari安装之Ambari安装前准备(CentOS6.5)(一) 第二步: Ambari安装之部署本地库(镜像服务器)(二) 第三步: Ambari安装之安装并配置Ambari-serv ...
- 关于开发板用tftp下载失败分析
一.想实现开发板和PC ping通:(1)windows和linux桥接(2)用路由器将开发板和PC连接起来(3)将windows和linux以及开发板的IP设置成同一网段,注意不要和你同一个局域网的 ...
- Django配置celery执行异步任务和定时任务
原生celery,非djcelery模块,所有演示均基于Django2.0 celery是一个基于python开发的简单.灵活且可靠的分布式任务队列框架,支持使用任务队列的方式在分布式的机器/进程/线 ...
- gensim学习笔记
1.词向量建模的word2vec模型和用于长文本向量建模的doc2vec模型 在Gensim中实现word2vec模型非常简单.首先,我们需要将原始的训练语料转化成一个sentence的迭代器:每一次 ...
- centos6 free 和 centos 7的free 的差异与对比
目录 一 centos6 free 常用参数和含义 centos6 free 命令示例 free 值讲解 计算公式 二 centos7 free 常用的参数 centos7 free 命令示例 计算公 ...