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 ...
随机推荐
- 非web工程,打jar放shell执行
作为6年经验的程序员,一直在搞web服务应用开发,今天领导被吐槽了,只会web方面的东东,最基本的打包啥啥都不会.. 一般开发工程都是web项目,突然要求开发非web,不用tomcat装(浪费端口号) ...
- VMware 虚拟化编程(4) — VDDK 安装
目录 目录 前文列表 VDDK 安装 VDDK 前文列表 VMware 虚拟化编程(1) - VMDK/VDDK/VixDiskLib/VADP 概念简析 VMware 虚拟化编程(2) - 虚拟磁盘 ...
- php扩展安装范例
php扩展安装: 安装bcmath: /usr/local/php/bin/phpize //指定路径 ./configure //可查找路径 ./configure --with-php-confi ...
- git_01_上传第一个项目至git
前言 Git是一个开源的分布式版本控制系统,可以有效.高速地处理从小到大的项目版本管理.最近在自己研究自动测试,也准备放到git上管理.由于工作中是在已有的代码库拉取.提交代码.自己想要初次建库上传项 ...
- mybatis如何把session托管给spring
原生mybatis创建SqlSession的流程: SqlSession sqlSession = null; try { sqlSession = sqlSessionFactory.openSes ...
- Java-多线程第四篇线程池
1.什么是线程池. 线程池在系统启动的时候即创建大量的空闲的线程,程序将一个Runnable对象或者Callable对象传给线程池,线程池就会启动一个线程来执行它们的run()或者call()方法,当 ...
- 初学css display
display:网上查到的资料说是:属性规定元素应该生成的框的类型.例如:网页上的导航栏,使用ul->li那么需要让其排列在一行上可以使用设置li:{float:left:},也可以使用disp ...
- 使用OFBIZ的理由和不使用OFBIZ的理由
1 使用OFBIZ的理由 1.1 什么是OFBIZ OFBIZ是由Sourceforge维护的一个最著名的开源项目之一,提供创建基于最新J2EE/XML规范和技术标准,构建大型企业级.跨平台.跨数据库 ...
- Hibernate快速入门之CRUD
一.Hibernate的基本概念 前言 Hibernate是一个ORM框架,ORM是Object Relational Mapping的缩写,即对象关系映射,也就是将面向对象思想的编程语言与关系型数据 ...
- javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful
一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...