leetcode 664. Strange Printer

博客介绍了一个奇怪打印机问题,打印机每次只能打印相同字符序列,且可覆盖原字符。给定小写英文字母字符串,需计算打印所需最小次数。解题采用动态规划和深度优先搜索,给出了状态转移方程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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[ i ][ j ] 表示打印[i j]区间字符序列所用最小次数)

  1. 可能每次只打印一个字符s[i] ,那么dp[i][j] = 1 + DFS(s ,  i + 1 , j) ;
  2. 对于[i j]之间的任一个字符s[k] , 如果s[k] == s[i] , 那么可以先将[i k]区间都打印成s[i]字符 ,再打印[i + 1 , k - 1 】 和 [k + 1 , j ]区间 ,所以dp[i][k] = dp[i][k - 1] , 那么dp[i][j] = min(dp[i][j] + DFS(s , i , k - 1) + DFS(s , k + 1 ,  j) ;
class Solution {
public:
    int strangePrinter(string s) 
    {
        dp = vector<vector<int>>(s.size() , vector<int>(s.size() , 0)) ;
        
        return DFS(s , 0 , s.size() - 1 ) ;
    }
    
    int DFS(string& s , int i , int j)
    {
        if( i > j ) return 0 ;
        
        if(dp[i][j] > 0) return dp[i][j] ;
        
        dp[i][j] = 1 + DFS(s , i + 1 , j) ;
        
        for(int k = i + 1 ; k <= j ; k++)
        {
            if(s[k] == s[i]) dp[i][j] = min(dp[i][j] , DFS(s , i , k - 1) + DFS(s , k + 1 , j)) ;
        }
        
        return dp[i][j] ;
    }
    
private :
    
    vector<vector<int>> dp ;
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值