leetcode 664. Strange Printer
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'.
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的更多相关文章
- LeetCode 664. Strange Printer 奇怪的打印机(C++/Java)
题目: There is a strange printer with the following two special requirements: The printer can only pri ...
- 664. Strange Printer
class Solution { public: int dp[100][100]; int dfs(const string &s, int i,int j) { if(i>j)ret ...
- [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 ...
- Java实现 LeetCode 664 奇怪的打印机(DFS)
664. 奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符 ...
- Leetcode 664.奇怪的打印机
奇怪的打印机 有台奇怪的打印机有以下两个特殊要求: 打印机每次只能打印同一个字符序列. 每次可以在任意起始和结束位置打印新字符,并且会覆盖掉原来已有的字符. 给定一个只包含小写英文字母的字符串,你的任 ...
- 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 ...
随机推荐
- python练习——小程序
1.打印0-10(while/for) count = 0 while count < 11: print(count) count += 1 for i in range(11): print ...
- 活动预告丨易盾CTO朱浩齐将出席2018 AIIA大会,分享《人工智能在内容安全的应用实践》
本文来自网易云社区 对于很多人来讲,仿佛昨天才燃起来的人工智能之火,转眼间烧遍了各个角落,如今我们的生活中,处处渗透着人工智能.10月16日,2018年 AIIA人工智能开发者大会在苏州举办,网易云易 ...
- js总结(一):javascript的类型:基本类型、对象和数组
javascript 类型分为2种,一个是原始值,另一个是复杂值(对象). 一.原始值 5个原始值是:数字,字符,布尔,null,undefined. 9个原生的对象构造函数:Number Strin ...
- Linux Notes:Linux下的远程登录协议及软件
常见的远程登录协议 1.RDP(remote desktopp protocol)协议,windows远程桌面协议 2.telnet CLI 界面下远程管理,几乎所有的操作系统都有,数据明文传输,不安 ...
- (4)主成分分析Principal Component Analysis——PCA
主成分分析Principal Component Analysis 降维除了便于计算,另一个作用就是便于可视化. 主成分分析-->降维--> 方差:描述样本整体分布的疏密,方差越大-> ...
- hdu 1325数据弱
#include<stdio.h>//判断是否有环,判断是否有点,判断是否是一个父节点 #include<string.h> #define N 1000000 int pre ...
- HDU 2629 Identity Card
简单题 给出身份证号 判断住址 和出生年月 熟练字符串的操作 主要是string::substr(s, l)//s:起始位置 l长度 #include <iostream> #includ ...
- 转 Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
转自: http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html 黄聪:Python 字符串操作(string替换.删除.截取. ...
- msp430入门编程43
msp430中C语言的人机交互--菜单公共函数
- mysql针对转义字符的模糊搜索
由于urlencode之后会产生很多'%'符号,这个符号在mysql模糊搜索中代表任意字符,显示会出现问题,例如 name字段经过urlencode之后变成‘%E6%9D%8E%E5%87%A1’,如 ...