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'.

Hint: Length of the given string will not exceed 100.

思路:
区间dp
区间dp顾名思义就是 区间上的dp,也就是从小到大枚举区间,每个区间就是一个最优子结构,由小到大地推得到最后的期间【0,n-1】

本题可以需要注意,如果两个区间a和b 如果a的开始和b的开始位置字符相同 那么 合并之后 需要的代价为 a+b - 1
举个例子
字符串abba 对于[0,2]和[3,3]区间,如果这两个区间合并的话 那么需要代价是2+1 -1. 为什么要减去1呢,题目中已经说了。。。
代码如下:

class Solution {
public:
int strangePrinter(string s) {
int dp[][];
int n = s.length();
if (n == ) return ;
memset(dp, 0x3f3f3f3f, sizeof dp);
for (int i = ; i < n; ++i) dp[i][i] = ;
for (int p = ; p < n; ++p) {
for (int i = ; i < n - p; ++i) {
int j = i + p;
for (int k = i; k <= j; ++k) {
int w = dp[i][k] + dp[k + ][j];
if (s[i] == s[k + ]) --w; // 第一段的起点 和第二段的起点一样 执行--
dp[i][j] = min(dp[i][j], w);
}
}
}
return dp[][n - ];
}
};

leetcode 664. Strange Printer的更多相关文章

  1. LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)

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

  2. 664. Strange Printer

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

  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. Java实现 LeetCode 664 奇怪的打印机(DFS)

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

  7. Leetcode 664.奇怪的打印机

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

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. MacOS & iOS

    MacOS & iOS https://github.com/qinjx/30min_guides/blob/master/ios.md https://www.cnblogs.com/xgq ...

  2. HDU4135容斥原理

    #include <cstdio> #include <string.h> #include <cmath> using namespace std; #defin ...

  3. 收集整理Android开发所需的Android SDK、开发中用到的工具、Android开发教程、Android设计规范,免费的设计素材等。

    AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...

  4. Bzoj 2726 SDOI 任务安排

      Memory Limit: 131072KB   64bit IO Format: %lld & %llu Description 机器上有N个需要处理的任务,它们构成了一个序列.这些任务 ...

  5. POJ3150:Cellular Automaton

    题意看不懂加题目想不通,很菜. n<=500个数围城环,每次操作对每个数Ai把与i在环上相距不超过d<n/2(包括Ai)的数加起来取模m<=1e6,求K<=1e7次操作后的环. ...

  6. Max Sum Plus Plus-HDU1024(dp)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  7. 动态规划:HDU 1114 Piggy-Bank

    Problem Description Before ACM can do anything, a budget must be prepared and the necessary financia ...

  8. BZOJ——2697: 特技飞行

    http://www.lydsy.com/JudgeOnline/problem.php?id=2697 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: ...

  9. java的异常与记录日志

    今天在<java编程思想>一书中看到了异常与记录日志,发现学会将异常记录进日志中还是很有必要的,以下是书中的例子: import java.io.PrintWriter; import j ...

  10. R-Tree空间索引算法的研究历程和最新进展分析

    转自原文 R-Tree空间索引算法的研究历程和最新进展分析,2008 摘要:本文介绍了空间索引的概念.R-Tree数据结构和R-Tree空间索引的算法描述,并从R-Tree索引技术的优缺点对R-Tre ...