Leetcode_1278. Palindrome Partitioning III_[DP]
You are given a string s
containing lowercase letters and an integer k
. You need to :
- First, change some characters of
s
to other lowercase English letters. - Then divide
s
intok
non-empty disjoint substrings such that each substring is palindrome.
Return the minimal number of characters that you need to change to divide the string.
Example 1:
Input: s = "abc", k = 2
Output: 1
Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome.
Example 2:
Input: s = "aabbc", k = 3
Output: 0
Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome.
Example 3:
Input: s = "leetcode", k = 8
Output: 0
Constraints:
1 <= k <= s.length <= 100
.s
only contains lowercase English letters.
解法:
由Leetcode_132. Palindrome Partitioning II解法,这道题可以有一个比较清晰的思路。
动态规划:dp[idx][k]表示将s[idx : s.size())这个子串划分为k个非空回文子串最少需要改变的字符数。
递归关系:dp[0][k] = min( f(0,idx-1) + dp[idx][k-1]), for idx in [0, s.size()-1],
其中 f(0,idx-1) == 0 if(s[0,idx-1] is palendrome),else f(0,idx-1) == number of chars that have to change.
需要注意,对于dp[idx][k],如果s.size()-idx < k,那么无论如何也不能将s[idx, s.size()-1]划分为k个回文子串。
class Solution {
public:
vector<vector<bool>> is_palindrome;
vector<vector<int>> dp;
int palindromePartition(string s, int k) {
int len = s.size();
is_palindrome = std::move(vector<vector<bool>>(len, vector<bool>(len, false)));
dp = std::move(vector<vector<int>>(len, vector<int>(k+, INT_MAX)));
for(int l=; l<=len; l++)
for(int head=; head+l-<len; head++){
int tail = head+l-;
if(l == )
is_palindrome[head][head] = true;
else if(l == )
is_palindrome[head][tail] = s[head]==s[tail];
else
is_palindrome[head][tail] = (s[head]==s[tail] && is_palindrome[head+][tail-]);
} dfs(s, , k);
return dp[][k];
} int dfs(string &s, int idx, int k){
if(idx == s.size())
return k== ? : s.size();
if(dp[idx][k]<INT_MAX)
return dp[idx][k];
int ret = s.size()+;
for(int l=; idx+l-+k-<s.size(); l++){
int behind = dfs(s, idx+l, k-), use = ;
if(!is_palindrome[idx][idx+l-]){
for(int head=idx, tail=idx+l-; head<tail; head++, tail--)
use += s[head]!=s[tail] ? : ;
}
ret = min(ret, use+behind);
}
return dp[idx][k] = ret;
}
};
Leetcode_1278. Palindrome Partitioning III_[DP]的更多相关文章
- 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_[DP]
题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...
- 分割回文串 · 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 ...
- Leetcode: Palindrome Partitioning II
参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
随机推荐
- npm构建vue项目
环境搭建 我们需要先从node.js官网安装node,安装过程很简单,一路“下一步”就可以了(傻瓜式安装). 安装完成之后,打开命令行工具(Mac打开终端),输入 node -v,如图,如果出现相应的 ...
- 精灵图和base64如何选择
Css Sprites: 介绍: Css Sprites(雪碧图或css精灵),是网页图片处理的一种方式,它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图 ...
- AI会议网站
<麻省理工科技评论>新兴科技峰会EmTech China : http://www.emtechchina.cn/ IT大咖说 各种科技前沿会议发布站 : http://www.itdks ...
- 396. Coins in a Line III
刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...
- (4.12)mysql备份还原——mysql逻辑备份之mysqldump
关键词:mysql逻辑备份介绍,mysqldump,mysqldump最佳实践 我的相关文章:https://www.cnblogs.com/gered/p/9721696.html 正文 1.mys ...
- git 中添加用户名和密码
git 中添加用户名和密码:https://blog.csdn.net/qq_28602957/article/details/52154384 在使用git时,如果用的是HTTPS的方式,则每次提交 ...
- P3452 [POI2007]BIU-Offices
传送门 首先能想到 $n^2$ 的做法 枚举所有两点,看看是否有边相连,如果没有说明它们一定要在同一集合,用并查集维护一下就行 注意到如果没有边这个条件,其实就相当于问补图有边 所以题意可以转化为,求 ...
- django信号相关
Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 1.Django内置信号 Model signals pre_in ...
- redis 安装和运行
安装redis 在CentOs虚拟机上:yum install redis,可能会出现以下错误: 解决方式:先输入yum install epel-release,之后输入:redis-server启 ...
- jQuery学习总结02-属性
1.attr(name|properties|key,value|fn) 说明:设置和返回被选元素的属性值 示例: 参数: name(属性名称) string properties(作为属性的'名/值 ...