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和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
随机推荐
- Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems
Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems 如果要给一对 "源控件" 和 " ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_09 序列化流_6_练习_序列化集合
- 2 Hadoop集群安装部署准备
2 Hadoop集群安装部署准备 集群安装前需要考虑的几点硬件选型--CPU.内存.磁盘.网卡等--什么配置?需要多少? 网络规划--1 GB? 10 GB?--网络拓扑? 操作系统选型及基础环境-- ...
- SqlServer 主重复制
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...
- vue-router路由如何实现传参
tip: 用params传参,F5强制刷新参数会被清空,用query,由于参数适用路径传参的所以F5强制刷新也不会被清空.(传参强烈建议适用string) 也可以选用sessionstorage/lo ...
- jQuery源码分析系列——来自Aaron
jQuery源码分析系列——来自Aaron 转载地址:http://www.cnblogs.com/aaronjs/p/3279314.html 版本截止到2013.8.24 jQuery官方发布最新 ...
- JAVA总结--集合
1.集合树状图 Collection:最基本的集合接口 ----List:有序集合,集合中的元素可以重复,访问集合中的元素可以根据元素的索引来访问 ----ArrayList:异步 ----Linke ...
- 从建立yum仓库到搭建ftp以及http服务
1 什么是yum仓库 yum工作需要依赖C/S架构工作模式的文件服务器,服务器中存放了yum工作时所需的程序包.yum接收到需要安装的程序包的名称之后,通过文件共享协议(或者文件传输协议),在配置文件 ...
- <每日一题> Day3:CodeForces-1141B.MaximalContinuousRest(简单题)
题目链接 参考代码: #include <iostream> #include <algorithm> using namespace std; + ; int value[m ...
- [2019杭电多校第一场][hdu6578]Blank(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 计数问题想到dp不过分吧... dp[i][j][k][w]为第1-i位置中4个数最后一次出现的 ...