leetcode 664. Strange Printer

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[110][110];
        int n = s.length();
        if (n == 0) return 0;
        memset(dp, 0x3f3f3f3f, sizeof dp);
        for (int i = 0; i < n; ++i) dp[i][i] = 1;
        for (int p = 1; p < n; ++p) {
            for (int i = 0; i < n - p; ++i) {
                int j = i + p;
                for (int k = i; k <= j; ++k) {
                    int w = dp[i][k] + dp[k + 1][j];
                    if (s[i] == s[k + 1]) --w; // 第一段的起点 和第二段的起点一样 执行--
                    dp[i][j] = min(dp[i][j], w);
                }
            }
        }
        return dp[0][n - 1];
    }
};
时间: 2024-10-10 06:06:18

leetcode 664. Strange Printer的相关文章

664. Strange Printer

class Solution { public: int dp[100][100]; int dfs(const string &s, int i,int j) { if(i>j)return 0; if(dp[i][j])return dp[i][j]; dp[i][j]=dfs(s,i,j-1)+1; for(int k=i;k<j;++k) { if(s[k]==s[j])dp[i][j]=min(dp[i][j],dfs(s,i,k)+dfs(s,k+1,j-1)); } re

[LeetCode] 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 t

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】动态规划(下篇共39题)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)

Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653-656) 链接:https://leetcode.com/contest/leetcode-weekly-contest-44 比赛情况记录:就做出来两题,第三题不难,然而就是在算坐标的时候卡住了.orz.结果:2/4,ranking:637/2272.第四题没看题,第三题搞得心情不好了orz. [653]Two Sum

【LeetCode】动态规划(下篇)

[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offers [639] Decode Ways II [646] Maximum Length of Pair Chain [647] Palindromic Substrings [650] 2 Keys Keyboard [651] 4 Keys Keyboard [656] Coin Path [6

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

LeetCode: Reverse Nodes in k-Group [024]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4648 Magic Pen 6 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1857    Accepted Submission(s): 637 Problem Description In HIT, many people have