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 ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_02 递归_1_递归概念&分类&注意事项
a方法里面调用自己,但是没有停止的条件 方法没有停止的条件. 栈内存溢出的异常. 只有栈,没有堆内存 先执行main方法压栈执行 main方法里面调用a方法.a方法就会压栈 改成20000
- KETTLE——(一)资源库
对KETTLE有了大概的了解,pdi-ce-6.0.1.0-386也下载完成了. 1.解压pdi-ce-6.0.1.0-386.zip文件,双击运行Spoon.bat(KETTLE是Java开发的,运 ...
- Iview 启动报错 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function
解决 fs.write(fd, buf, 0, buf.length, 0, function(err, written, buffer) {}); 替换为 fs.write(fd, buf, 0, ...
- MyBatis Generator 生成的example 使用 and or 简单混合查询
MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...
- ArrayList,linkedList vecator的实现和区别
1.线程安全问题. ArrayList 和 linkedList 线程是不安全的,而vecator是线程安全的. 因为ArrayList 和 linkedList 是线程不同步的,vecator是同步 ...
- 000 (H5*) 常见代码
目录: 1:HTML 1:块级元素(block-level) 2:行内元素(inline-level) 3:行内块元素(inline-block) 4: img标签 5:表单元素 6:自定义列表 d ...
- python基础-6.1 match search findall group(s) 区别
import re # match findall经常用 # re.match() #从开头匹配,没有匹配到对象就返回NONE # re.search() #浏览全部字符,匹配第一个符合规则的字符串 ...
- Java集合概括总结及集合选用
Collection 1. List ArrayList :Object数组,是一个数组队列,相当于动态数组.它由数组实现,随机访问效率高,随机插入.随机删除效率低. LinkedList :是一个双 ...
- python 二维数组转一维数组
三种方法 比如 a = [[1, 2], [3, 4], [5, 6]] 列表推导式 [i for j in a for i in j] 库函数 from itertools import chain ...
- Python之字符串和正则表达式
使用正则表达式 Python对正则表达式的支持 例子:替换字符串中的不良内容 import re def main(): sentence = '你丫是傻叉吗? 我操你大爷的. Fuck you.' ...