LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)
题目:
There is a strange printer with the following two special requirements:
- The printer can only print a sequence of the same character each time.
- At each turn, the printer can print new characters starting from and ending at any places, and will cover the original existing characters.
Given a string consists of lower English letters only, your job is to count the minimum number of turns the printer needed in order to print it.
Example 1:
Input: "aaabbb"
Output: 2
Explanation: Print "aaa" first and then print "bbb".
Example 2:
Input: "aba"
Output: 2
Explanation: Print "aaa" first and then print "b" from the second place of the string, which will cover the existing character 'a'.
分析:
有一个打印机,每次只能打印一种字符,同时打印机可以在任意起始位置打印字符,同时会覆盖掉已有的字符。
利用这个打印机,求出打印所给字符串所需的最少打印次数。
我们先来看最坏情况,也就是一个一个打印,那么例如aabba这个字符串时需要5次的,但是我们可以注意到,字符串最后的a,如果前面同样有a这个字符的话,就可以在前一次打印时,将a打印好了,也就是我们可以先打印aaaaa,然后再打印b将a覆盖掉,也就是aabba这样也就只需要两次。
所以我们可以递归求解此问题,求字符串s所需要的打印次数,我们可以先默认打印次数等于除去最后一个字符的字符串所需的次数+1,也就是认为最后一个字符需要单独打印一次,
print(s(i,j)) = print(s(i,j-1))+1
然后我们遍历前面的字符串中的字符,当有字符和最后一个字符相同时,意味着我们可以考虑在前一次打印时,直接将最后一个字符顺带打印出来,我们记字符索引为k,也就是在打印s(i,k)的一步时把最后一个字符打印出来,那么此时打印次数=print(s(i,k)) + print(s(k+1,j-1)),再去和默认打印的次数比较求最小值即可。每次求得的打印字串需要的次数记录下来,调用时直接返回即可。
程序:
C++
class Solution {
public:
int strangePrinter(string s) {
if(s == "")
return ;
int m = s.size();
times = vector<vector<int>>(m, vector<int>(m, ));
return step(s, , m-);
}
private:
int step(string &s, int i, int j){
if(i > j)
return ;
if(i == j)
return ;
if(times[i][j] > )
return times[i][j];
int res = step(s, i, j-) + ;
for(int k = i; k < j; ++k)
if(s[k] == s[j])
res = min(res, step(s, i, k) + step(s, k+, j-));
times[i][j] = res;
return res;
}
vector<vector<int>> times;
};
Java
class Solution {
public int strangePrinter(String s) {
if(s.length() == 0)
return 0;
int l = s.length();
times = new int[l][l];
return step(s, 0, l-1);
}
private int step(String s, int i, int j){
if(i > j)
return 0;
if(i == j)
return 1;
if(times[i][j] > 0)
return times[i][j];
int res = step(s, i, j-1) + 1;
for(int k = i; k < j; ++k)
if(s.charAt(k) == s.charAt(j))
res = Math.min(res, step(s, i, k) + step(s, k+1, j-1));
times[i][j] = res;
return res;
}
private int[][] times;
}
LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)的更多相关文章
- [LeetCode] Strange Printer 奇怪的打印机
There is a strange printer with the following two special requirements: The printer can only print a ...
- leetcode 664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- 664. Strange Printer
class Solution { public: int dp[100][100]; int dfs(const string &s, int i,int j) { if(i>j)ret ...
- [Swift]LeetCode664. 奇怪的打印机 | Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- Java实现 LeetCode 664 奇怪的打印机(DFS)
664. 奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符 ...
- Leetcode 664.奇怪的打印机
奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符串,你的任 ...
- LeetCode664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...
- HDU 1548 A strange lift 奇怪的电梯(BFS,水)
题意: 有一座电梯,其中楼层从1-n,每层都有一个数字k,当处于某一层时,只能往上走k层,或者下走k层.楼主在a层,问是否能到达第b层? 思路: 在起点时只能往上走和往下走两个选择,之后的每层都是这样 ...
- LeetCode刷题时引发的思考:Java中ArrayList存放的是值还是引用?
好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 今天我在刷LeetCode ...
随机推荐
- 惊呆了!Java程序员最常犯的错竟然是这10个
和绝大多数的程序员一样,我也非常的宅.周末最奢侈的享受就是逛一逛技术型网站,比如说 programcreek,这个小网站上有一些非常有意思的主题.比如说:Java 程序员最常犯的错竟然是这 10 个, ...
- 用Eclipse和Tomcat搭建一个本地服务器
服务器软件环境 Eclipse oxygen Tomcat 9.0 SQL Sever 2014 参考资料 https://blog.csdn.net/qq_21154101/article/deta ...
- Django常用字段及参数、事务、数据库查询优化
常用字段 注意: Django中没有设置对应char类型的字段,但可以支持自己定义. 自定义对应于数据库的char类型字段: from django.db.models import Field cl ...
- 基于JGraphT实现的路径探寻
基于JGraphT实现的路径探寻 业务中提出基于内存,探寻的两点间的有向以及无向路径,多点间的最小子图等需求,以下记录使用JGraphT的实现过程. GraphT是免费的Java类库,提供数学图论对象 ...
- 一些常用查询SQL语句以及显示格式
1.查询当前年.月.周相关时间 1.1.查询当前年份 SELECT TO_CHAR(SYSDATE,'YYYY') AS YEAR FROM DUAL--查询当前年份 SELECT TO_CHAR(S ...
- Spring学习记录5——数据库事务基础知识
何为数据库事务 “一荣共荣,一损共损”这句话很能体现事务的思想,很多复杂的事务要分步进行,但它们组成了一个整体,要么整体生效,要么整体失效.这种思想反映到数据库上,就是多条SQL语句,要么全部成功,要 ...
- dp - 逆序数序列
对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数 ...
- 算法笔记codeup-Contest100000568
A #include <stdio.h> int main() { ; ; while(a) { sum=sum+a; a--; } printf("%d",sum); ...
- 关于neo4j初入门(4)
关于admin管理员 数据库备份和恢复 Neo4j数据库备份 步骤1 -点击“Neo4j Community”,如下图所示 Windows“开始”按钮>> "All Progra ...
- Webpack实战(六):如何优雅地运用样式CSS预处理
上一篇文章中,我主要分享了<Webpack如何分离样式文件>CSS 预处理器是一个能让你通过预处理器自己独有的语法来生成CSS的程序,css预处理指的是在开发中我们经常会使用一些样式预编译 ...