Leetcode_132. Palindrome Partitioning II_[DP]
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
Example:
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
题意同样简单明了。
解法:
由上题Palindrome Partitioning,可以得到一个解此题的思路,DFS穷举所有方案。然而,仅仅是DFS会超时。此题仅要求求得划分次数最小的方法的划分次数。在这个问题下,dfs(string &s, int idx) 返回 s[idx, s.size()-1] 这个子串最少需要多少次划分。可以发现,朴素DFS存在大量的重复计算,因为s[0, idx)子串有多少种划分法,dfs(string &s, int idx) 就会被调用多少次,而每次计算 dfs(string &s, int idx) 返回的结果都是相同的。因此,自然而然的想到记忆化搜索,也就是dp。
class Solution {
public:
vector<vector<bool>> is_palindrome;
vector<int> dp;
int minCut(string s) {
int len = s.size();
is_palindrome = std::move(vector<vector<bool>>(len, vector<bool>(len, false)));
dp = std::move(vector<int>(len, len+));
for(size_t l=; l<=len; l++)
for(size_t head=; head+l-<len; head++){
int tail = head+l-;
if(l == )
is_palindrome[head][tail] = true;
else if(s[head]==s[tail] && (head == tail- || is_palindrome[head+][tail-]))
is_palindrome[head][tail] = true;
}
int ret = len;
dfs(s, );
return dp[]-;
}
int dfs(string& s, int hp){
if(hp == s.size())
return ;
if(dp[hp] < s.size()+)
return dp[hp];
int ret = s.size()+;
for(size_t i=s.size()-hp; i>=; i--)
if(is_palindrome[hp][hp+i-])
ret = min(ret, +dfs(s, hp+i));
return dp[hp] = ret;
}
};
Leetcode_132. Palindrome Partitioning II_[DP]的更多相关文章
- Leetcode_1278. Palindrome Partitioning III_[DP]
题目链接 You are given a string s containing lowercase letters and an integer k. You need to : First, ch ...
- 131. Palindrome Partitioning (Back-Track, DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- Atcoder Yet Another Palindrome Partitioning(状压dp)
Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...
- 132. Palindrome Partitioning II (String; DP)
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Lightoj 1044 - Palindrome Partitioning (DP)
题目链接: Lightoj 1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
随机推荐
- pom.xml文件设置
一个相对完整的maven配置文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- Linux_VMWare12 Install RHEL7
目录 目录 前言 Install RHEL7 前言 准备考试,顺便来一波VMWare安装虚拟机的图文详解. Install RHEL7 step1. 选择自定义安装,Next step2. 版本兼容性 ...
- json 格式
Json格式规则:(Douglas Crockford提出的) 1) 并列的数据之间用逗号(“,”)分隔. 2) 映射用冒号(“:”)表示. 3) 并列数据的集合(数组)用方括号("[]&q ...
- Appium-入门实例1
参考:(https://blog.csdn.net/zh175578809/article/details/76862590) 第一步:启动虚拟设备 在运行App之前,首先需要创建一个Android模 ...
- 应用安全 - PHPCMS - Joomla漏洞汇总
Joomla 反序列化(版本低于3.4.5) CVE-2015-8562 RCE Date:October, 2019原理:https://blog.hacktivesecurity.com/inde ...
- 006/搭建fabric(二)
准备vmware虚拟机,并安装完ubuntu系统后.继续搭建fabric运行环境... 0.打开终端,切换root身份.目的:后续操作即可不用sudo... 右键->open Terminal- ...
- Implement Queue using Stacks(用两个栈实现队列)
来源:https://leetcode.com/problems/implement-queue-using-stacks Implement the following operations of ...
- 自己挖的坑自己填--docker创建实例出现Waiting for SSH to be available…
在之前使用Docker for Windows Installer.exe直接安装,通过docker-machine-driver-vmwareworkstation.exe实现docker和VM的共 ...
- 设置国内AndriodSDK代理
由于一些原因,Google相关很多服务都无法访问,所以在很多时候我们SDK也无法升级,当然通过技术手段肯定可以解决,但是比较麻烦,而且下载速度也不怎么样. 这里笔者介绍一个国内的Android镜像站, ...
- JAVA总结--泛型
泛型 :程序设计语言的一种特性:将类型参数化: 特征:凡是涉及到强制类型转化的地方,使用泛型均会编译出现问题:泛型仅仅在编译时进行校验,使用泛型的对象,其本质的类型依然不变: ps:不存在泛型数组 一 ...