题目链接

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. Jmeter之事物控制器

    在我们需要统计一组取样器的统计数据,可以将这一组取样器放置在事物控制器下,进行统计. 一.界面显示 二.配置说明 1.名称:标识 2.注释:备注 3.Generate parent sample: 不 ...

  2. JSONPath解析json

    JSONPath - 用于JSON的XPath 用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具. 考虑到接下来计划开发一个自动化测试平台,在 ...

  3. 【ABAP系列】SAP ABAP解析XML的示例程序

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP解析XML的示例 ...

  4. 使用SqlBulkCopy 批量操作大量数据

    private void button1_Click(object sender, EventArgs e) { //1.0 构建一个内存表一定要和Users表的结构保持一致,除了自增的主键外 Dat ...

  5. 20190906 On Java8 第十八章 字符串

    第十八章 字符串 +的重载与StringBuilder 用于String的+与+=是Java中仅有的两个重载过的操作符,Java不允许程序员重载任何其他的操作符.编译器自动引入了java.lang.S ...

  6. Yarn 工作机制

    1.工作机制详述 (1)MR程序提交到客户端所在的节点. (2)YarnRunner向ResourceManager申请一个Application. (3)RM将该应用程序的资源路径返回给YarnRu ...

  7. [BZOI 3994] [SDOI2015]约数个数和(莫比乌斯反演+数论分块)

    [BZOI 3994] [SDOI2015]约数个数和 题面 设d(x)为x的约数个数,给定N.M,求\(\sum _{i=1}^n \sum_{i=1}^m d(i \times j)\) T组询问 ...

  8. 手写ORM持久层框架(转)

    工程结构: 本文测试的数据库为: 其中student的表结构为: 表数据: 配置文件 DB2.properties driver=com.mysql.jdbc.Driver url=jdbc\:mys ...

  9. C++析构函数的自动调用(析构函数必须是虚拟的,这样删除父类指针指向的子类对象,才能同时调用两者的析构函数,否则就没有机会调用子类析构函数)

    class A {public: A() { printf("A \n"); } ~A() { printf(" ~A \n"); } // 这里不管写不写vi ...

  10. web前端工程化

    目标 1.能够了解模块化的相关规范 2.了解webpack 3.了解使用Vue单文件组件 4.能够搭建Vue脚手架 5.掌握Element-UI的使用 1.模块化的分类 A.浏览器端的模块化 1).A ...