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. luoguT21778 过年

    差分一下上线段树 #include <iostream> #include <cstdio> #include <vector> using namespace s ...

  2. MySQL语句之or和and多条件查询

    在where中可以包含任意数目的and和or操作符,需要注意的是在没有任何其他符号的时候,例如括号,AND的优先级高于OR,因此,当两者一起使用时,先运算AND两边的条件表达式,再运算OR两边的条件表 ...

  3. NYOJ-487月老的烦恼(1)类似于素数筛法一样的打表及一种筛法核心代码。。

    月老的烦恼(1) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 月老最近遇到了一个很棘手的问题,就是"剩男""剩女"急速增长,而 ...

  4. Linux使用Mutt发送邮件/附件

    使用Mutt发邮件极其方便,只需要一条命令即可发送或者批量发送邮件 功能说明:E-mail管理程序. 语 法:mutt [-hnpRvxz][-a<文件>][-b<地址>][- ...

  5. 机器学习基础-Logistic回归1

    利用Logistic回归进行分类的主要思想是:根据现有数据对分类边界线建立回归公式,以此进行分类. 训练分类器时的做法就是寻找最佳拟合参数,使用的时最优化算法. 优点:计算代价不高,利于理解和实现. ...

  6. ZOJ 3811 Untrusted Patrol【并查集】

    题目大意:给一个无向图,有些点有装监视器记录第一次到达该点的位置,问是否存在一条路径使得监视器以给定的顺序响起,并且经过所有点 思路:牡丹江网络赛的题,当时想了种并查集的做法,通神写完程序WA了几发, ...

  7. 【博弈+GCD】C. Alice and Bob

    https://www.bnuoj.com/v3/contest_show.php?cid=9147#problem/C [题意] 初始时有n个数,定义操作为从n个数中取出两个数x,y,如果|x-y| ...

  8. python监控tomcat日记文件

    最近写了一个用python监控tomcat日记文件的功能 实现的功能: 监控日记文件中实时过来的记录,统计每分钟各个接口调用次数,统计结果插入oracle #!/usr/bin/python # -* ...

  9. 【frameset】frameset设置不能拖动

    <frameset rows='20%,*' >           <!--  row 行 col 列    分行列要为rows  cols  --> <frame s ...

  10. Python基础之 一 文件操作

    文件操作 流程: 1:打开文件,得到文件句柄并赋值给一个变量 2:通过句柄对文件进行操作 3:关闭文件 模式解释 r(读) , w(写) ,a(附加)r+(读写的读), w+(读写的写),a+(读附加 ...