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.

For example, given s ="aab",
Return1since the palindrome partitioning["aa","b"]could be produced using 1 cut.

题意:给定字符串s,分割s,使其每个子串都是回文串,至少需要切几下。

思路:动态规划。维护数组dp[ ] ,dp[ i ]代表(0 ,i)最小的回文切割。遍历字符串,当子串s.substr(0,i+1)(包括 i 位置的字符)是回文时,dp[i]=0,即表示不用切割,若不是回文,则令dp[ i ]=i ,表示至少要切 i 刀(有i+1个字符)。对于任意大于1的 i,如果s.substr(j,i+1)(j<=i,即遍历i之前的每个子串)

是回文时,转移方程:dp[i]=min(dp[i],dp[j-1]+1),因为若是,则只要增加一刀,就可以分为两个子串了;若不是,则取dp[i]=min(dp[i],dp[i-1]+1),因为,为回文串时,状态转移方程中j-1>=0,说明,首字符没有考虑,若出现如“efe”的情况时,dp[i] 的值就小于dp[ i-1 ]+1。代码如下:

 class Solution {
public:
int minCut(string s)
{
int len=s.size();
if(len<) return ; vector<int> dp(len,); for(int i=;i<len;i++)
{
if( !isPalin(s,,i))
dp[i]=i;
} for(int i=;i<len;++i)
{
for(int j=;j<i;j++)
{
if(isPalin(s,j,i))
dp[i]=min(dp[i],dp[j-]+);
else
dp[i]=min(dp[i],dp[i-]+);
}
}
return dp[len-];
} bool isPalin(string &s,int l,int r)
{
int i=l,j=r;
while(i<j)
{
if(s[i] !=s[j])
return false;
i++;
j--;
}
return true;
}
};

博友Grandyang,使用DP简化了每次第(j,i+1)之间的是否为回文串的判断。

[Leetcode] palindrome partition ii 回文分区的更多相关文章

  1. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  2. 【CF932G】Palindrome Partition(回文树,动态规划)

    [CF932G]Palindrome Partition(回文树,动态规划) 题面 CF 翻译: 给定一个串,把串分为偶数段 假设分为了\(s1,s2,s3....sk\) 求,满足\(s_1=s_k ...

  3. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

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

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

  5. [LeetCode] Palindrome Linked List 回文链表

    Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time ...

  6. [LeetCode] Palindrome Number 验证回文数字

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  7. [Leetcode] Palindrome number 判断回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  8. [LeetCode] 409. Longest Palindrome 最长回文

    Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...

  9. LeetCode 9 Palindrome Number(回文数字判断)

    Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的 ...

随机推荐

  1. PHP一些常用魔术方法

    魔术方法                          调用方法                                     作用__set                   有两个 ...

  2. php sign签名实例

    1:实现签名代码: /** * 签名生成算法 * @param array $params API调用的请求参数集合的关联数组,不包含sign参数 * @param string $secret 签名 ...

  3. JavaSE基础复习---Class类与反射机制

    ---恢复内容开始--- 目录: 1.java.lang.class类 2.Java中的反射机制 3.运行时与编译时概念 1. java.lang.class类 Java程序在运行时,Java运行时系 ...

  4. ubuntu下安装redis及在php中使用

    一.安装redis sudo apt-get install redis-server 安装完成后,Redis服务器会自动启动,我们可以通过下面的命令行检查一下: # redis-cli > p ...

  5. 【IOI 2002/FJOI2019】任务安排(超级计算机)

    题目 \(N\) 个任务排成一个序列在一台机器上等待完成(顺序不得改变),这 \(N\) 个任务被分成若干批,每批包含相邻的若干任务.从时刻 \(0\) 开始,这些任务被分批加工,第 \(i\) 个任 ...

  6. FJOI 2019 游记

    (FJOI 2019 滚粗记) Day 0 早上刷了一些水题,然后就上路了. (画图3D好好玩) 来得晚只有十几分钟时间看考场,于是连试机题都没有Ak. Day 1 果然我还是太菜了 走过来的时候再讨 ...

  7. 用gradle编译任意结构的Android项目

    ## 需求 * 继续用`Eclipse`项目的结构,但是使用`gradle`编译,或者说任意的项目结构进行编译. ## 解决方案 1. Android studio的项目结构 1. Android S ...

  8. 自动化测试---mybatis的使用

    mybatis如何实现了对数据库的操作: 1.通过Resources.getResourceAsReader()或者 Resources.getResourceAsStream()加载mybatis. ...

  9. 数据库学习(二) case when then else end 的使用

    case函数还用来统计数据的,参考资料:https://www.cnblogs.com/qlqwjy/p/7476533.html 这里我只是整理下工作中使用的到案例: 查询语句: SELECT t. ...

  10. 12.0 Excel表格读取

    Pycharm安装 xlrd 首先在xuexi目录下创建一个ExcelFile文件,让后在ExcelFile下创建一个Excel表格 创建表格时记得把单元格的格式设置为[文本] 我们设置为文本之后,存 ...