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. [转载]Linux 16进制查看命令、工具

    转自:https://blog.csdn.net/chenglian_999/article/details/4672177 2009年10月14日 21:45:00 chenglian_999 阅读 ...

  2. Navicat Premium 12激活

    大自然的搬运工:https://www.jianshu.com/p/5f693b4c9468 声明:本文所提供的所有软件均来自于互联网,个人存放在此作为备用,以备将来不时之需,同时作为大家的分享和学习 ...

  3. esay-ui学习笔记(一)

    JavaScript prototype用法 prototype 属性使您有能力向对象添加属性和方法. object.prototype.name=value <script type=&quo ...

  4. sony Z5P 刷rec、root的方法

    想root需要刷第三方recovery,刷recovery需要先解锁.但如果直接解锁,会丧失相机算法.屏幕超逼真模式,所以不能直接来. 大体步骤就是解完锁后自己做个内核刷进去,欺骗系统让他觉得没解锁. ...

  5. js +与?优先级

    var val = 'smtg'; console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing'); // A. Value ...

  6. 正则化,L1,L2

    机器学习中在为了减小loss时可能会带来模型容量增加,即参数增加的情况,这会导致模型在训练集上表现良好,在测试集上效果不好,也就是出现了过拟合现象.为了减小这种现象带来的影响,采用正则化.正则化,在减 ...

  7. [术语] CRUD 增删改查

    Data Manipulation Language, DML 数据操纵语言Insert update delete CRUD :create read update delete

  8. Fastjson-fastjson中$ref对象重复引用问题

    当你有城市数据,你需要按国内.国际.热门城市分成数组的形式给出并输出为json格式. 第一个问题,你的数据格式,需要按字母类别划分,比如: "int": { "C&quo ...

  9. Android中使用Thread线程与AsyncTask异步任务的区别

    最近和几个朋友交流Android开发中的网络下载问题时,谈到了用Thread开启下载线程时会产生的Bug,其实直接用子线程开启下载任务的确是很Low的做法,那么原因究竟如何,而比较高大上的做法是怎样? ...

  10. java对redis的基本操作,ZZ

    java对redis的基本操作 http://www.cnblogs.com/edisonfeng/p/3571870.html