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和回溯法了,但是这些暴力的方法加上剪枝之后复杂度依然是很 ...
随机推荐
- 山西汽车销量(hive)
1.创建数据库create database db_cart; 2.使用数据库use db_cart; 3.创建表create table t_cart(province STRING,month I ...
- 【opencv】opencv图像识别的一些基础的基础函数的使用方法
import cv2 import numpy as np from matplotlib import pyplot as plt pic_path = "deal_with.png&qu ...
- oracle--增删改、表备份、集合运算
truncate 插入数据学习及数据的备份 单表查询语句(select) 1.插入数据(insert) 1.语法规范 insert into 表名(字段1,字段2,字段3,....)values('值 ...
- BZOJ 4987 (树形DP)
###题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4987 ###分析 先考虑贪心,显然k个节点形成一棵树 求出树的直径,显然直径应该只被经 ...
- HTML+CSS ,原型
此图是别人所作
- 23、前端知识点--webpack的使用详解
Webpack 是当下最热门的前端资源模块化管理和打包工具. https://www.cnblogs.com/zhangruiqi/p/7656206.html
- linux学习笔记(1):
一.Linux系统简介 1.什么是linux Linux是一个免费的.多用户.多任务的操作系统,其运行方式.功能和UNIX系统很相似,但Linux系统的稳定性.安全性与网络功能是许多商业操作系统所无法 ...
- 微信公众号获取微信token
微信在公众号和小程序的开发都有开放文档一般看文档开发就行,很简单这里写一个小demo获取微信token,之后根据自己的业务获取信息处理即可 package com.demo.ccx; import o ...
- JSP学习(5)
JSP学习(5) 保存用户状态的两大机制 session对象 Cookie Cookie简介 是Web服务器保存在客户端的一系列文本信息 典型应用 判断注册用户是否已经登录 购物车处理 作用 对特定对 ...
- 详解webpack4打包--新手入门(填坑)
注意,这个dev和build好像在哪儿见过??对了, 刚刚才在package.json里配置的“scripts”这一项的值就有“dev”和“build”.对,一点都不错,就是这2个值,这2个值代表的是 ...