class Solution {
public:
int dp[100][100];
int dfs(const string &s, int i,int j)
{
if(i>j)return 0;
if(dp[i][j])return dp[i][j];
dp[i][j]=dfs(s,i,j-1)+1;
for(int k=i;k<j;++k)
{
if(s[k]==s[j])dp[i][j]=min(dp[i][j],dfs(s,i,k)+dfs(s,k+1,j-1));
}
return dp[i][j];
}
int strangePrinter(string s) {
if(s.empty())return 0;
return dfs(s,0,s.size()-1);
}
};

  

1

我刚开始的思路是, 相邻的字符如果相同就去重, 然后循环 0 到 n, 首尾相等的话就不用额外打印;

例如  aaabbbaaa  先去重变为  aba , 循环第一个, a 打印一次,  第二个b 打印一次,  第三个a 发现和第一个相同, 不用打印;  然后提交时发现有些case没有通过测试;

后来总结了下, 我漏掉了回文的情况,  我发现这题用回文思路也能做, 因为打印回文的话, 回文总长度是n, 是不是最多n/2+1次就可以了,  例如  abcba=3次   abccba=3次  aaaa=1次, 不过我也懒得去实现回文算法来做比较了.

2

上面贴的答案是discussion讨论区的答案, 很标准的dfs+dp 解法,  代码很清晰, 几乎不需要解释,

大致思路就是 每新增一个字符, 尽可能想办法不要额外次数去打印, 那么不打印的办法就是看有没有办法和前面已经打印过得字符"合并",  这里极端的情况就是上面说到的回文啦

打印字符串第i个位置到第j个位置需要的次数  =  dp[i][j];

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

min左边的意思是不能合并, 只能再打印一次,  右边的意思是  找到一个位置k,  符合 i<k<j,  使得  i到k  这部分  +   k+1到j 这部分 之和最小

664. Strange Printer的更多相关文章

  1. leetcode 664. Strange Printer

    There is a strange printer with the following two special requirements: The printer can only print a ...

  2. LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)

    题目: There is a strange printer with the following two special requirements: The printer can only pri ...

  3. [LeetCode] Strange Printer 奇怪的打印机

    There is a strange printer with the following two special requirements: The printer can only print a ...

  4. [Swift]LeetCode664. 奇怪的打印机 | Strange Printer

    There is a strange printer with the following two special requirements: The printer can only print a ...

  5. LeetCode664. Strange Printer

    There is a strange printer with the following two special requirements: The printer can only print a ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. leetcode hard

    # Title Solution Acceptance Difficulty Frequency     4 Median of Two Sorted Arrays       27.2% Hard ...

随机推荐

  1. ssm回顾笔记(一)

    这两天来到了农银,这边即将进行的一个项目是将ssh框架的电商项目迁移到springboot+ssm框架上,所以我基本上是三门技术在同时进行学习,当然以前学过ssm,现在只是回顾. spring 注解 ...

  2. js没有函数重载

    上面这道题,要求判断输出的y和z分别为什么 一开始,我选择了2,4 后来发现答案是4,4 意识到js中没有函数重载!!!即使声明了两个同名函数,结果也是后面的函数覆盖了前一个函数. 而且函数声明会提升 ...

  3. Docker 搭建代码质量检测平台 SonarQube

    开始搭建 1.获取 postgresql 的镜像 $ docker pull postgres 2.启动 postgresql $ docker run --name db -e POSTGRES_U ...

  4. React项目中使用HighCharts

    大家都知道BizCharts是基于react封装的一套图表工具,而HighCharts是基于jQuery的.但是由于本人对BizCharts甚是不熟,所以在react项目开发中选择了HighChart ...

  5. Gson如何解析key值是数字的json数据

    1.使用注解@SerializedName来解决这个问题 2.比如有如下json数据:(key值是数字"1112") { "1112": "抖音&qu ...

  6. Unity --- OnValidate 和 ExecuteInEditMode

    using UnityEngine; [ExecuteInEditMode] //添加脚本.启动.Stop的时候执行Awake() Start() public class test : MonoBe ...

  7. Django+七牛上传+查看+下载文件相关函数,新整理未完全测试

    M class File(models.Model): # 文档模型 name = models.CharField(max_length=255) staff = models.ForeignKey ...

  8. springcloud-Ribbon-负载均衡组件

    Ribbon负载均衡 1.Ribbon简介 ribbin是Netflix发布的负载均衡器,有助于控制http和tcp客户端的行为,为ribbon配置服务提供者列表后,ribbon就可以基于某种负载均衡 ...

  9. 【HTML入门基础知识】从零开始,我要加油!---致HTML

    前言: 今天来和大家分享一下近期自己整理的HTML笔记,希望会对你的学习有所帮助! ***本章关键词:HTML头部格式:常见的块级标签:常见的行级标签:表格:表单. 一.HTML头部格式 一.HTML ...

  10. Android 音视频深入 十一 FFmpeg和AudioTrack播放声音(附源码下载)

    项目地址,求starhttps://github.com/979451341/AudioVideoStudyCodeTwo/tree/master/FFmpeg%E6%92%AD%E6%94%BE%E ...