题目链接

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 into k 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]的更多相关文章

  1. 131. Palindrome Partitioning (Back-Track, DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  2. Leetcode_132. Palindrome Partitioning II_[DP]

    题目链接 Given a string s, partition s such that every substring of the partition is a palindrome. Retur ...

  3. 分割回文串 · Palindrome Partitioning

    [抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...

  4. Atcoder Yet Another Palindrome Partitioning(状压dp)

    Atcoder Yet Another Palindrome Partitioning 思路: 一个字符串满足条件的情况是奇数字母个数小于等于1,也就是异或起来是1<<j(0<=j& ...

  5. 132. Palindrome Partitioning II (String; DP)

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. Lightoj 1044 - Palindrome Partitioning (DP)

    题目链接: Lightoj  1044 - Palindrome Partitioning 题目描述: 给一个字符串,问至少分割多少次?分割出来的子串都是回文串. 解题思路: 先把给定串的所有子串是不 ...

  7. [LeetCode] Palindrome Partitioning II 拆分回文串之二

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  8. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  9. Leetcode: Palindrome Partitioning II

    参考:http://www.cppblog.com/wicbnu/archive/2013/03/18/198565.html 我太喜欢用dfs和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...

随机推荐

  1. jmeter之三种参数化

    前言:总结并记录几种jmeter比较有用的元件 1.接口文档 2.参数化 3.断言 1.接口文档 a.拿到接口文档 接口地址:http://localhost:8080/jpress/admin/lo ...

  2. android:为ListView 添加自定义头部和尾部,上拉主动加载 .(引)

    1.加头尾部 (1)定自义一个xml布局 my_headview.xml 在Activity类里找到这个自定义布局,并添加到头部 LinearLayout  hearderViewLayout = ( ...

  3. Jmeter的中英文互换

    1.jmeter的中英文互换:为了更深入的了解Jmeter,一般使用英文版的jmeter. 旧版本下载的默认中文较多.新版本5.1的下载后默认英文比较的多. 方法一: 选项->选择一种语言,但是 ...

  4. Python笔记(二十三)_魔法方法_字符串的魔法方法

    __str__和__repr__ 如果要把一个类的实例变成字符串,就需要实现魔法方法__str__()和__repr__ >>>class A: def name(self): re ...

  5. IIS下如何配置免费的https

    1.首先我们要取走我们的证书,保存在我们本地的电脑里,然后复制到服务器即可. 2.取走后接下来干嘛?当然是打开文件看看里面有些什么啊.我们找到IIS那个压缩包并解压. 3.解析得到pfx文件,也就是我 ...

  6. 关于存储过程的一些sql

    1.关于事务的回滚Set XACT_ABORT ON; 开启为on时 ,如果事务语句产生运行错误 ,将整个事务终止进行回滚,Off时只回滚产生错误的语句. 2.获取事务语句中上一次插入值的行号@@ID ...

  7. Cors 跨域 共享

    CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...

  8. Node.js 的环境配置

    1.安装node.js 官网上面最新版本的nodejs 都是自带npm的.http://nodejs.cn/至于安装过程选择默认下载就行 2.安装淘宝镜像,也就是将国外的一些资源代理到国内. 步骤:1 ...

  9. WPF ControlTemplate

    ControlTemplate:控件模板,顾名思义也就是定制特定的控件供公共调用,有点类似WinForm中对一些通用控件进行重写使用. ControlTemplate:控件模板主要有两个重要属性:Vi ...

  10. python进阶之类的反射

    有应用场景的技术才是有灵魂的技术------>最近同时问我,在python中,给你一个函数或者类的字符串名称,你怎么得到该函数和类,以下结合源码记录我得到的方式: 1.给一个函数的字符串&quo ...