664. Strange Printer
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的更多相关文章
- leetcode 664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)
题目: There is a strange printer with the following two special requirements: The printer can only pri ...
- [LeetCode] Strange Printer 奇怪的打印机
There is a strange printer with the following two special requirements: The printer can only print a ...
- [Swift]LeetCode664. 奇怪的打印机 | Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- LeetCode664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
随机推荐
- Redis主主复制、主从复制
Redis主从复制(master-slaves配置)可以更好地读写分离 先关防火墙 主(master)服务器先不做任何操作, 进入从(slave)服务器, redis-3.2.1目录下的redis.c ...
- 【三】jquery之选择器
转自:https://www.cnblogs.com/youfeng365/p/5846650.html jquery参考手册:http://jquery.cuishifeng.cn/index.ht ...
- excel加密文件破解代码
1. AIT+F11 2. 代码 3. F5 Public Sub AllInternalPasswords()' Breaks worksheet and workbook structure ...
- 力扣(LeetCode)482. 密钥格式化
给定一个密钥字符串S,只包含字母,数字以及 '-'(破折号).N 个 '-' 将字符串分成了 N+1 组.给定一个数字 K,重新格式化字符串,除了第一个分组以外,每个分组要包含 K 个字##符,第一个 ...
- 《Practical Vim》第十章:复制和粘贴
第十章和第十一章讲了 Vim 的寄存器功能, 寄存器,是用于保存文本的特定的容器.它的内容: 既可以是类似于系统剪切板功能的,用于粘贴的文本 也可以是录制成的宏的命令. 第十章讲使用寄存器使用复制与粘 ...
- 通过class改变样式
1.判断class属性 判断一个元素中是否含有指定的class属性值 function hasClass(obj,cn){ //创建正则表达式 var reg=new RegExp("\\b ...
- Django框架(七)
15 Django组件-中间件 中间件 中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的 ...
- 【Sql】经典sql语句
参考网页:https://www.cnblogs.com/qixuejia/p/3637735.html 1./**查询课程1比课程2,成绩高的学生学号1.分析这些元素都在一个表里,但是上下两条记录, ...
- liunx文件操作 文件压缩
文件备份和压缩命令 在Linux中,常用的文件压缩工具有gzip,bzip2,zip. 'bzip2'是最理想的压缩工具,它提供了最大限度的压缩. 'zip'兼容好,windows也支持. bzip2 ...
- fiddler抓取用tomcat来部署的项目接口请求包
Fiddler 是以代理web服务器的形式工作的,它使用代理地址:127.0.0.1,端口:8888. 当Fiddler退出的时候它会自动注销, 这样就不会影响别的程序.关于fiddler这个工具的使 ...