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
sto other lowercase English letters. - Then divide
sintoknon-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.sonly 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和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
随机推荐
- EF6中一个关于时间类型 datetime2 的坑
在一个访问下位机的程序中,返回的时间戳有时候因断线产生0001年01月01日的时间,而原先使用拼接SQL进行数据存储的操作时,这个问题是可以跳过的. 这次把拼接SQL的部分重新改为EF进行管理,这个坑 ...
- jQuery测试错题解析
1. JavaScript中实现回车切换效果是利用了event对象的( )属性. A.Tab B.keyCode C.KeyCode D.KeyDown 解析:实现回车切换效果是keyCode属性.故 ...
- shell脚本一一项目6
主题:获取网卡的流量 ifconfig 查看流量 文件流量数据量 脚本内容 #!/bin/bash#name: mark# check network dev's liuliangnic=$1 ech ...
- rac的一次问题 ORA-01565: error in identifying file '+DATA/bol/spfilebol.ora'
昨天安装的测试环境的rac--2节点 CentOS release 6.8 (Final) SQL*Plus: Release 11.2.0.4.0 Production 今天测试突然出现问题 在ra ...
- ios-wx.chooseImage选择图片后,在ios中选中的图片未显示
解决办法:微信引用1.2.0的版本 http://res.wx.qq.com/open/js/jweixin-1.2.0.js 我之前用的版本是:http://res.wx.qq.com/open/j ...
- arduino相关文献阅读
首推这个 https://wenku.baidu.com/view/e657b1f0bcd126fff6050baf.html 用Arduino IDE开发程序流程 当程序编写好之后,关闭前需要将文件 ...
- 倾旋之slack主题协同
源:https://pocketcorp.slack.com/join/shared_invite/enQtNTk2MDYwNDA4NzU0LTg3ZGVlNDE5NWUzNjJhZTc1MDQ5MT ...
- python+selenium文本框对象以及按钮对象操作
文本框对象 from selenium import webdriverfrom time import sleep driver = webdriver.Firefox() # 指定和打开浏览器ur ...
- xml与Properties的区别
1.properties配置文件,是一个属性对应于一个值(key = value)这样的键值匹对模式: 每一行properties配置文件的键值,对应着一次赋值: 特殊点: 在前后两行properti ...
- C程序优化
1 优化内存使用 后期程序优化的时候看内存分配是否浪费:是否留有安全裕量:有些内存是否分配过多,有些内存是否分配了没使用:尤其是创建对象的时候