题目是:

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

Return tthe mininum cuts needed for a palindrome partitioning of s.

For example,given s="aab"

Return 1 since the  palindrome partition["aa","b"]could be produced using 1cut

我的思路:

用动态数组dp[i]来记录当i+1个字符时,需要的分隔次数。

当前i个字符是回文串时,则dp[i]=0

当前i个字符不是回文串时,则dp[i]先置为i(i+1个字符需要的最大分割次数)

这个时候的dp[i]的值就由前i个字符来决定,用j来分隔前i个字符

则dp[i]=min{dp[i],dp[j-1]+1(当前j个字符是回文串时)||dp[j-1]+1+i-j(当前j个字符不是回文串时)}

代码如下:

int minCut(string s){

  vector<int>dp(s,size(),s.size()-1)//默认值为字符串的最大分割次数

  for(int i=0;i<s.size();i++){

    dp[i]=Is_palindrome(s.substr(0,i+1))?0:i;//判断i+1个字符是不是回文串

    if(dp[i]==0)continue;//如果是则继续循环

    for(int j=1;j<=i;j++){

      if(Is_palindrome(s,substr(j,i+1-j))){

        dp[i]=min(dp[i],dp[j-1]+1);

      }

      else{

        dp[i]=min(dp[i],dp[j-1]+1+i-j);

      }

    }

  }

  return dp[s.size()-1];

}

//判断是否是回文串

bool Is_palindrome(string s){

  int begin=0;

  int end=s.size()-1;

  while(begin<end){

    if(s[begin]<s[end]){

      begin++;

      end--;

     }

    else break;

    if(begin>=end)return true;

    return false;

    }

}

做这条题目遇到的坑:

在判断回文串的时候,我首先想到的是STL中算法中的reverse函数,但是做的时候还是拿不准reverse函数的参数,于是采取了上面代码的方式,要是严格说起来的话也不是很难,清晰易懂,看来写代码不能拘泥于现成的东西。

leetcode刷题1--动态规划法回文串2的更多相关文章

  1. 【LEETCODE】72、分割回文串 III 第1278题

    package y2019.Algorithm.dynamicprogramming.hard; /** * @Auther: xiaof * @Date: 2019/12/11 08:59 * @D ...

  2. 【leetcode 简单】 第九十六题 最长回文串

    给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设字符串的长度不 ...

  3. [LeetCode] Longest Palindrome 最长回文串

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

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

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

  5. 从0打卡leetcode之day 6--最长回文串

    题目描述 给定一个字符串 s,找到 s中最长的回文子串.你可以假设 s 的最大长度为1000. 示例1 输入: "babad" 输出: "bab" 注意: &q ...

  6. [LeetCode] 214. Shortest Palindrome 最短回文串

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. 牛客寒假算法基础集训营4 I题 Applese 的回文串

    链接:https://ac.nowcoder.com/acm/contest/330/I 来源:牛客网 自从 Applese 学会了字符串之后,精通各种字符串算法,比如--判断一个字符串是不是回文串. ...

  8. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. leetcode.字符串.409最长回文串-Java

    1. 具体题目 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设 ...

  10. [LeetCode] Largest Palindrome Product 最大回文串乘积

    Find the largest palindrome made from the product of two n-digit numbers. Since the result could be ...

随机推荐

  1. suse 12 二进制部署 Kubernetets 1.19.7 - 第13章 - 部署metrics-server插件

    文章目录 1.13.0.创建metrics-server证书和私钥 1.13.1.生成metrics-server证书和私钥 1.13.2.开启kube-apiserver聚合配置 1.13.3.分发 ...

  2. 树莓派GPIO开发(一):激光头传感器模块的使用

    配置环境 系统:Raspbian11(64位) 设备:树莓派CM4 一.写在前面 主要为了测试我捡漏买的CM4的拓展版 拓展板子没有焊接引脚,但是预留的接口 手动焊接一下 测试成功 ,说明我捡的这块板 ...

  3. 字符集编码(三):Unicode

    前面<字符集编码(上):Unicode 之前>我们讲了在二十世纪九十年代 Unicode 出现之前各厂商和标准化组织为了应对不同语言文字的编码需求而设计了各种互不兼容的字符集编码标准,这使 ...

  4. RIP协议测试——信而泰网络测试仪实操

    一.简介: RIP(Routing Information Protocol,路由信息协议)是一种内部网关协议(IGP),是一种动态路由选择协议,用于自治系统(AS)内的路由信息的传递.RIP协议基于 ...

  5. 常用windows快捷键及cmd、dos命令

    Windows常用快捷键 #Alt+F4:关闭窗口.网页 #ctrl+C:复制 #ctrl+V:粘贴 #ctrl+X:剪切 #ctrl+Z:撤销 #ctrl+A:全选 #ctrl+S:保存 #shif ...

  6. vmware扩容centos根目录

    在vmware中编辑,给磁盘扩容 在centos中使用命令fdisk /dev/sda 输入n创建新分区 输入p创建主分区 回车,默认分区号 回车,默认起始扇区 回车,默认last扇区 输入t,改变分 ...

  7. 60天shell脚本计划-2/12-渐入佳境

    --作者:飞翔的小胖猪 --创建时间:2021年2月1日 --修改时间:2021年2月5日 说明 每日上传更新一个shell脚本,周期为60天.如有需求的读者可根据自己实际情况选用合适的脚本,也可在评 ...

  8. 5.string字符串

    string(字符串)是 Redis 中最简单的数据类型.我们知道,Redis 所有数据类型都是以 key 作为键,通过检索这个 key 就可以获取相应的 value 值.Redis 存在多种数据类型 ...

  9. XML序列化反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Xml.Serialization; nam ...

  10. linux-日常工作积累

    Linux常用命令之envsubst https://blog.csdn.net/banche163/article/details/101369495 Linux中的EAGAIN含义 https:/ ...