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));
        }
        return dp[i][j];
    }
    int strangePrinter(string s) {
        if(s.empty())return 0;
        return dfs(s,0,s.size()-1);
    }
};

  

1

我刚开始的思路是, 相邻的字符如果相同就去重, 然后循环 0 到 n, 首尾相等的话就不用额外打印;

例如  aaabbbaaa  先去重变为  aba , 循环第一个, a 打印一次,  第二个b 打印一次,  第三个a 发现和第一个相同, 不用打印;  然后提交时发现有些case没有通过测试;

后来总结了下, 我漏掉了回文的情况,  我发现这题用回文思路也能做, 因为打印回文的话, 回文总长度是n, 是不是最多n/2+1次就可以了,  例如  abcba=3次   abccba=3次  aaaa=1次, 不过我也懒得去实现回文算法来做比较了.

2

上面贴的答案是discussion讨论区的答案, 很标准的dfs+dp 解法,  代码很清晰, 几乎不需要解释,

大致思路就是 每新增一个字符, 尽可能想办法不要额外次数去打印, 那么不打印的办法就是看有没有办法和前面已经打印过得字符"合并",  这里极端的情况就是上面说到的回文啦

打印字符串第i个位置到第j个位置需要的次数  =  dp[i][j];

dp[i][j] = min (dp[i][j-1]+1, dp[i][k]+dp[k+1[[j-1])   i<k<j;

min左边的意思是不能合并, 只能再打印一次,  右边的意思是  找到一个位置k,  符合 i<k<j,  使得  i到k  这部分  +   k+1到j 这部分 之和最小

原文地址:https://www.cnblogs.com/lychnis/p/9247920.html

时间: 2024-10-10 06:06:28

664. Strange Printer的相关文章

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 t

[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】动态规划(下篇)

[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】动态规划(下篇共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】深搜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周赛】从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

[2016-04-20][codeforces][664][A][Complicated GCD]

时间:2016-04-20 22:57:26 星期三 题目编号:[2016-04-20][codeforces][664][A][Complicated GCD] 题目大意:求a~b之间的所有数字的最大公约数 分析:相邻两个数字,最大公约数只能是1,然后相同就是本身 #include<cstdio> #include<cstring> using namespace std; const int maxn = 150; char s1[maxn],s2[maxn]; int mai