作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/

题目描述

Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.

Example 1:

Input: s1 = "sea", s2 = "eat"
Output: 231 Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.

Example 2:

Input: s1 = "delete", s2 = "leet"
Output: 403 Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum. Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.

Note:

  1. 0 < s1.length, s2.length <= 1000.
  2. All elements of each string will have an ASCII value in [97, 122].

题目大意

给出两个字符串,可以在每个字符串中删除一些字符,得到相等的字符串。求删除的字符的ASCII最小和。

解题方法

看到玩字符串+最优问题,一定是DP没错了。我们已经做过了求LCS的问题,当时的dp的结果是个数。这个题改成ASCII就好。思路和我们583. Delete Operation for Two Strings基本一致。

583这个题的做法是求个数,所以每个位置如果相等的话,就+1,而这个题求ASCII,所以相等的话加上ASCII。

对于 DP 的问题,最重要的是找到合适的状态和状态转移方程。其实直接使用LCS的ASCII之和作为状态就行了。

题目所求为使得两个字符串 ascii 和相同的最小删除字符 ascii 和,所以我们设 dp[i][j] 为 s1 前 i 个字符与 s2 前 j 个字符得到LCS所需的ASCII和。

那么我们开始构造转移方程:

对于 s1[0…i−1] 和 s2[0…j−1] 的 LCS的ASCII的和应该是这样的:

  1. 若 s1[i−1]==s2[j−1] ,则 dp[i][j]=C[i−1][j−1] + ord(s1[i-1])
  2. 若不相等,则 s1[i−1], s2[j−1] 选择删除一个,
    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

这里应该还是比较容易理解的,即LCS的字符数不变。

最终的结果和583一样,要把所有的和减去LCS的和。

代码:

class Solution(object):
def minimumDeleteSum(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: int
"""
l1, l2 = len(s1), len(s2)
dp = [[0] * (l2 + 1) for _ in range(l1 + 1)]
for i in range(1, l1 + 1):
for j in range(1, l2 + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + ord(s1[i - 1])
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
result = sum(map(ord, s1 + s2)) - dp[-1][-1] * 2
return result

C++版本的char直接转成int就是得到了ASCII码,所以简单一点。

C++代码如下:

class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
const int M = s1.size(), N = s2.size();
vector<vector<int>> dp(M + 1, vector<int>(N + 1, 0));
for (int i = 1; i < M + 1; i ++)
dp[i][0] = dp[i - 1][0] + s1[i - 1];
for (int j = 1; j < N + 1; j ++)
dp[0][j] = dp[0][j - 1] + s2[j - 1];
for (int i = 1; i < M + 1; i ++) {
for (int j = 1; j < N + 1; j ++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]);
}
}
}
return dp[M][N];
}
};

参考:

  1. https://blog.csdn.net/bowen_wu_sysu/article/details/78428635
  2. https://leetcode.com/problems/delete-operation-for-two-strings/discuss/103214/Java-DP-Solution-(Longest-Common-Subsequence)

日期

2018 年 4 月 4 日 —— 清明时节雪纷纷~~下雪了,惊不惊喜,意不意外?
2018 年 12 月 14 日 —— 12月过半,2019就要开始

【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)的更多相关文章

  1. LN : leetcode 712 Minimum ASCII Delete Sum for Two Strings

    lc 712 Minimum ASCII Delete Sum for Two Strings 712 Minimum ASCII Delete Sum for Two Strings Given t ...

  2. [LeetCode] 712. Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  3. LeetCode 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  4. LC 712. Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  5. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  6. 712. Minimum ASCII Delete Sum for Two Strings

    题目: Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings eq ...

  7. Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings)

    Leetcode之动态规划(DP)专题-712. 两个字符串的最小ASCII删除和(Minimum ASCII Delete Sum for Two Strings) 给定两个字符串s1, s2,找到 ...

  8. [LeetCode] Minimum ASCII Delete Sum for Two Strings 两个字符串的最小ASCII删除和

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

  9. [Swift]LeetCode712. 两个字符串的最小ASCII删除和 | Minimum ASCII Delete Sum for Two Strings

    Given two strings s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal. ...

随机推荐

  1. centos安装后的个人工具

    1.安装vim工具 yum -y install vim 安装完成后在家目录下新建一个.vimrc的配置文件.辅助vim软件功能. set number " 显示行号 set cursorl ...

  2. Hive(十三)【Hive on Spark 部署搭建】

    Hive on Spark 官网详情:https://cwiki.apache.org//confluence/display/Hive/Hive+on+Spark:+Getting+Started ...

  3. 零基础学习java------21---------动态代理,java8新特性(lambda, stream,DateApi)

    1. 动态代理 在一个方法前后加内容,最简单直观的方法就是直接在代码上加内容(如数据库中的事务),但这样写不够灵活,并且代码可维护性差,所以就需要引入动态代理 1.1 静态代理实现 在讲动态代理之前, ...

  4. Angular 中 [ngClass]、[ngStyle] 的使用

    1.ngStyle 基本用法 1 <div [ngStyle]="{'background-color':'green'}"></<div> 判断添加 ...

  5. 【Elasticsearch-Java】Java客户端搭建

    Elasticsearch Java高级客户端   1.  概述 Java REST Client 有两种风格: Java Low Level REST Client :用于Elasticsearch ...

  6. mysql explain using filesort

    创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...

  7. OSGI 生命周期

    1 生命周期管理 对于非模块化应用,生命周期将应用作为一个整体来操作: 而对于模块化应用,则可以以细粒度的方式来管理应用的某一个独立部分. OSGi生命周期管理 OSGi生命周期层有两种不同的作用: ...

  8. 【Java多线程】Java 中断

    如何安全的结束一个正在运行的线程 java.lang.Thread类包含了一些常用的方法,如:start(), stop(), stop(Throwable) ,suspend(), destroy( ...

  9. 莫烦python教程学习笔记——learn_curve曲线用于过拟合问题

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  10. JS 双向数据绑定、单项数据绑定

    简单的双向数据绑定 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...