题目链接

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

Return the minimum cuts needed for a palindrome partitioning of s.

Example:

Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.

题意同样简单明了。

解法:

由上题Palindrome Partitioning,可以得到一个解此题的思路,DFS穷举所有方案。然而,仅仅是DFS会超时。此题仅要求求得划分次数最小的方法的划分次数。在这个问题下,dfs(string &s, int idx) 返回 s[idx, s.size()-1] 这个子串最少需要多少次划分。可以发现,朴素DFS存在大量的重复计算,因为s[0, idx)子串有多少种划分法,dfs(string &s, int idx) 就会被调用多少次,而每次计算 dfs(string &s, int idx) 返回的结果都是相同的。因此,自然而然的想到记忆化搜索,也就是dp。

class Solution {
public:
vector<vector<bool>> is_palindrome;
vector<int> dp;
int minCut(string s) {
int len = s.size();
is_palindrome = std::move(vector<vector<bool>>(len, vector<bool>(len, false)));
dp = std::move(vector<int>(len, len+));
for(size_t l=; l<=len; l++)
for(size_t head=; head+l-<len; head++){
int tail = head+l-;
if(l == )
is_palindrome[head][tail] = true;
else if(s[head]==s[tail] && (head == tail- || is_palindrome[head+][tail-]))
is_palindrome[head][tail] = true;
}
int ret = len;
dfs(s, );
return dp[]-;
} int dfs(string& s, int hp){
if(hp == s.size())
return ;
if(dp[hp] < s.size()+)
return dp[hp];
int ret = s.size()+;
for(size_t i=s.size()-hp; i>=; i--)
if(is_palindrome[hp][hp+i-])
ret = min(ret, +dfs(s, hp+i));
return dp[hp] = ret;
}
};

Leetcode_132. Palindrome Partitioning II_[DP]的更多相关文章

  1. Leetcode_1278. Palindrome Partitioning III_[DP]

    题目链接 You are given a string s containing lowercase letters and an integer k. You need to : First, ch ...

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

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

  3. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

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

  4. 分割回文串 · Palindrome Partitioning

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

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

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

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

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

  7. Lightoj 1044 - Palindrome Partitioning (DP)

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

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

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

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

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

随机推荐

  1. Ajax 实现导出文件-支持批量

    个人感觉前端不行,好多东西记不住,所以只能将遇到的坎以及解决方案记录下,方便以后用到时查找. 首先:ajax不支持流,网上找了好多版本,感觉下面的方案不错,实验了下可行. 前端页面: <div ...

  2. springAOP基于注解的使用方法和实现原理

     springAOP即面向切面编程,可以在方法执行过程中动态的织入增强逻辑,其使用步骤为: 1. 导入aop模块的jar包,或在maven中添加依赖:spring-aspects 2. 定义目标类和目 ...

  3. mkdir: 无法创建目录"kk": 只读文件系统

    创建文件提示 root@hap1:/test>mkdir kk mkdir: 无法创建目录"kk": 只读文件系统 root@hap1:/test>mount .... ...

  4. Hadoop实战内容摘记

    Hadoop 开源分布式计算平台,前身是:Apache Nutch(爬虫),Lucene(中文搜索引擎)子项目之一. 以Hadoop分布式计算文件系统(Hadoop Distributed File ...

  5. KETTLE——(三)数据输出

    数据输出和数据输入基本差不多,KETTLE本身支持的数据输出方式也特别多,还是以数据库输出为例. ​ 打开表输出的界面,简单介绍一下其功能: ​ 就这个界面,如果不勾选[指定数据库字段],KETTLE ...

  6. Java 基础-基本数据类型与表达式

    基本数据类型 基本概念 标识符 标识符与内存中的某个位置对应,Java 中标识符的规范如下: 必须由大小写字母.下划线.美元符号.数字组成 首字母只能是大小写字母.下划线.美元符号 变量 变量的值可以 ...

  7. Cors 跨域 共享

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

  8. 如何通过脚本实现显示版本号、CPU、硬盘和内存条大小

    COLOR="\033[1;$[RANDOM%7+31]m"     COLOR1="\033[1;$[RANDOM%7+31]m"COLOR2="\ ...

  9. python之getopt

    getopt可以分析输入的参数,根据不同的参数输入不同的命令 getopt.getopt( [命令行参数列表], "短选项", "长选项列表" ) getopt ...

  10. VBA中如何用environ$ 或 environ方法取得环境变量?

    用索引号取得环境变量Sub EnumSEVars() Dim strVar As String Dim i As Long strVar = Environ$(i) & Then Exit F ...