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 ... 
随机推荐
- xtu summer individual 4 C - Dancing Lessons
			Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ... 
- zoj 2829 Beautiful Number
			Beautiful Number Time Limit: 2 Seconds Memory Limit: 65536 KB Mike is very lucky, as he has two ... 
- Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现
			 Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现 LayerDrawable实现的结果和附录文章1,2,3中的layer-list一致. ... 
- Archiving not possible: No primary destinations errors
			If space ran out in an archive destination, after you fix the problem, you may still recieve the fol ... 
- [luoguP2982][USACO10FEB]慢下来Slowing down(dfs序 + 线段树)
			传送门 这个题显然可以用树链剖分做. 然而线段树也能做. 每个点都对它的子树有贡献,所以先求一边 dfs序,然后直接在 dfs序 中搞 线段树 就行. ——代码 #include <cstdio ... 
- [HDU2896]病毒侵袭(AC自动机)
			传送门 题目中文描述,赞! 除了val记录id以外就是模板. 注意:每次数组都要清0.0 ——代码 #include <cstdio> #include <queue> #in ... 
- BZOJ 3175: [Tjoi2013]攻击装置
			捉水题真是捉上瘾了TUT Description 给定一个01矩阵,其中你可以在0的位置放置攻击装置.每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y- ... 
- js1:对象的学习,构造函数,继承构造函数【使用教材:JavaScript深度剖析第2版】
			原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <title>js</title> & ... 
- MySQL Slow Log慢日志分析【转】
			如果你的MySQL出现了性能问题,第一个需要“诊断”的就是slow log(慢日志)了. slow log文件很小,使用more less等命令就足够了.如果slow log很大怎么办?这里介绍MyS ... 
- JAVA_构造方法
			构造方法: 作用:是给对象的数据进行初始化用的. 特点:1 必须和类的名字一样. 2 和真正的方法是有所不同的,构造方法是没有返回值的类型的 eg: 方法:public void User ( ... 
