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 ...
随机推荐
- 【搬运】Wget 命令详解
用过 Linux 系统的对于 wget 不陌生吧,从网上下载资源等操作都是少不了它,它体积小但功能集全,支持 FTP HTTP HTTPS 协议下载方式,支持断点续传 代理服务器. 现在 Window ...
- 【转载】Selenium WebDriver的简单操作说明
转载自:http://blog.csdn.net/xiao190128/article/details/49784121 1.打开一个测试浏览器 对浏览器进行操作首先需要打开一个浏览器,接下来才能对浏 ...
- Index.get_indexer 方法的含义
表示,to_match 中的字符,在 unoque_vals 中的位置索引
- 全文检索:haystack+elasticsearch
优点: 1.查询速度快 2.支持中文分词准备工作:安装es软件 1.拷贝到ubuntu 2.docker load -i 文件路径 3.配置 修改ip地址 4.docker run -dti --ne ...
- 20175317 《Java程序设计》第三周学习总结
20175317 <Java程序设计>第三周学习总结 教材学习内容总结 第三周我学习了教材第四章的内容,了解了Java中的部分常用语句,学到了以下内容: 明白了什么是类,成员变量有哪些,什 ...
- JWT ajax java spingmvc 简洁教程
1.添加依赖 <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</ ...
- mean
import caffe import numpy as np MEAN_PROTO_PATH = 'mean.binaryproto' # 待转换的pb格式图像均值文件路径 MEAN_NPY_PAT ...
- Set集合中的treeSet问题:cannot be cast to java.lang.Comparable;
使用TreeSet保存自定义对象时, 必须让定义对象的类实现Comparable接口,并重写compareTo()方法 否则报 实体类User:cannot be cast to java.lang. ...
- Matlab:线性热传导(抛物线方程)问题
函数文件1:real_fun.m function f=real_fun(x0,t0) f=(x0-x0^2)*exp(-t0); 函数文件2:fun.m function f=fun(x0,t0) ...
- 【LeetCode】不同路径
如图,m × n 的网格的左上角作为起点,每次只能向右或向下移动一格,最终要到达右下角.求有多少条可能的路径. m,n 最大取 100. 我的想法是递归,分分钟实现 int uniquePaths(i ...