题目:

There is a strange printer with the following two special requirements:

  1. The printer can only print a sequence of the same character each time.
  2. 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)的更多相关文章

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

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

  2. leetcode 664. Strange Printer

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

  3. 664. Strange Printer

    class Solution { public: int dp[100][100]; int dfs(const string &s, int i,int j) { if(i>j)ret ...

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

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

  5. Java实现 LeetCode 664 奇怪的打印机(DFS)

    664. 奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符 ...

  6. Leetcode 664.奇怪的打印机

    奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符串,你的任 ...

  7. LeetCode664. Strange Printer

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

  8. HDU 1548 A strange lift 奇怪的电梯(BFS,水)

    题意: 有一座电梯,其中楼层从1-n,每层都有一个数字k,当处于某一层时,只能往上走k层,或者下走k层.楼主在a层,问是否能到达第b层? 思路: 在起点时只能往上走和往下走两个选择,之后的每层都是这样 ...

  9. LeetCode刷题时引发的思考:Java中ArrayList存放的是值还是引用?

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 今天我在刷LeetCode ...

随机推荐

  1. 一次 kafka 消息堆积问题排查

    收到某业务组的小伙伴发来的反馈,具体问题如下: 项目中某 kafka 消息组消费特别慢,有时候在 kafka-manager 控制台看到有些消费者已被踢出消费组. 从服务端日志看到如下信息: 该消费组 ...

  2. Java截图小程序源码

    Java编写的全屏截图小程序 package cnom.test.testUtils; import java.awt.AWTException; import java.awt.Dimension; ...

  3. C#实现的对文件的重命名

    如下C#实现对文件的重命名的方法需要传入三个string类型的参数,分别是源文件的文件目录.目的文件目录和重命名的文件名称,实现代码如下: public ExecutionResult FileRen ...

  4. vnpy源码阅读学习(4):自己写一个类似vnpy的UI框架

    自己写一个类似vnpy的界面框架 概述 通过之前3次对vnpy的界面代码的研究,我们去模仿做一个vn.py的大框架.巩固一下PyQt5的学习. 这部分的代码相对来说没有难度和深度,基本上就是把PyQt ...

  5. day6 相对定位:position:relative

    相对定位:position:relative 特点:a.相对于自己原来位置的定位,以自己的左上角为基准. b.相对定位原来的位置仍然算位置,不会出现浮动现象. 以下为初始位置:(可以看出设置margi ...

  6. Sigmoid非线性激活函数,FM调频,胆机,HDR的意义

    前几天家里买了个二手车子,较老,发现只有FM收音机,但音响效果不错,车子带蓝牙转FM,可以手机蓝牙播放音乐,但经过几次转换以及对FM的质疑,所以怀疑音质是否会剧烈下降,抱着试试的态度放了一个手机上的音 ...

  7. Zero down time upgrade with OGG -from 11g to 12c.

    High level steps upgrade from 11g to 12c database: 1)    Check network between source and target. 2) ...

  8. Win10如何关闭最近使用文件夹

    如果默认开启了最近使用文件夹功能的话,最近浏览的文件就会被记录下来.所以看完各种教学资料后,并不是关掉或者重启别人就不知道你看了什么. 方法一: Win10怎么关闭最近使用文件夹 如何清理使用痕迹 从 ...

  9. xlwings excel(二)

    常用函数和方法 Book 工作簿常用的apiwb=xw.books[‘工作簿名称'] wb.activate()激活为当前工作簿 wb.fullname 返回工作簿的绝对路径 wb.name 返回工作 ...

  10. CentOS7.0+Hadoop2.7.2+Hbase1.2.1搭建教程

    1.软件版本 CentOS-7.0-1406-x86_64-DVD.iso jdk-7u80-linux-x64.tar.gz hadoop-2.7.2.tar.gz hbase-1.2.1-bin. ...