作者: 负雪明烛
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. nginx二级域名指向不同文件项目配置

    需要使用泛域名解析, 并且加上空的判断,以保证没有二级域名的也可以访问 核心配置 server_name ~^(?<subdomain>.+)\.caipudq\.cn$;if ( $su ...

  2. mongodb-to-mongodb

    python3用于mongodb数据库之间倒数据,特别是分片和非分片之间. 本项目是一个集合一个集合的倒. 参考了logstash,对于只增不减而且不修改的数据的可以一直同步,阻塞同步,断点同步.改进 ...

  3. 年底巩固下 CS 知识「GitHub 热点速览 v.21.49」

    作者:HelloGitHub-小鱼干 期末到了!是时候来一波 CS 复习资料了,从本科基础知识开始到实用编程技术.本周 GitHub 热点趋势榜给你提供了最全的复习资料:清华的 CS 四年学习资料.W ...

  4. css通配样式初始化(多款,供君自选)

    腾讯官网 body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0; ...

  5. HTML5 之 FileReader 的使用 (二) (网页上图片拖拽并且预显示可在这里学到) [转载]

    转载至 : http://www.360doc.com/content/14/0214/18/1457948_352511645.shtml FileReader 资料(英文): https://de ...

  6. 【leetcode】451. Sort Characters By Frequency

    Given a string s, sort it in decreasing order based on the frequency of the characters. The frequenc ...

  7. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(五)-文件管理初步介绍

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...

  8. JAVA中的六种日期类型使用

    基本的6种日期类 /** * 六种时间类型的类 * 数据库格式的时间三种格式 */ java.util.Date date = new java.util.Date();//年与日时分秒 //数据库的 ...

  9. shell获取目录下(包括子目录)所有文件名、路径、文件大小

    一例shell脚本:取得目录下(包括子目录)所有文件名.路径与文件大小. 代码,shell脚本: lsdir.sh #!/bin/bash # #site: www.jquerycn.cn funct ...

  10. html之table的tr加间隔

    <table style="border-collapse:separate; border-spacing:0px 10px;"> <tr> <td ...