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:

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

两个字符串最长子序列。

Runtime: 12 ms, faster than 76.47% of C++ online submissions for Minimum ASCII Delete Sum for Two Strings.

#include <iostream>
#include <string>
#include <string.h>
using namespace std;
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int dp[s1.size()+][s2.size()+];
memset(dp, , sizeof(dp));
int sum = ;
for(int i=; i<s1.size(); i++){
sum += (int)s1[i];
for(int j=; j<s2.size(); j++){
if(s1[i] == s2[j]) {
dp[i+][j+] = dp[i][j] + (int)s1[i];
}else {
dp[i+][j+] = max(dp[i+][j], dp[i][j+]);
}
}
}
for(int i=; i<s2.size(); i++){
sum += (int)s2[i];
}
return sum - *dp[s1.size()][s2.size()];
}
};

LC 712. Minimum ASCII Delete Sum for Two Strings的更多相关文章

  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 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

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

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

  5. 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 ...

  6. 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. ...

  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. mysql 中的 tinyint 字段

    只能存储  -128 ~ 127  之间的数字

  2. WLW模板插件Text Templat的应用举例

    WLW的模板插件:WLWTextTemplates 安装之后,如下图所示: 点击这个按键之后,出现下图: 按上图提示点击"Add new Template",出现下图:   举个例 ...

  3. 序列化 json pickle shelve configparser

    一 什么是 序列化 在我们存储数据或者 网络传输数据的时候,需要对我们的 对象进行处理,把对象处理成方便我们存储和传输的 数据格式,这个过程叫序列化,不同的序列化,结果也不相同,但是目的是一样的,都是 ...

  4. 【vue lazyload】插件的使用步骤

    VUE图片懒加载-vue lazyload插件的简单使用

  5. View相关面试问题-View绘制面试问题详解

    View树的绘制流程: measure()  -->  layout() --> onDraw(),具体使用这个可以参考之前实现的自定义的博客:[http://www.cnblogs.co ...

  6. linux抓取usb包设置usbmon

  7. 从二叉查找树到平衡树:avl, 2-3树,左倾红黑树(含实现代码),传统红黑树

    参考:自平衡二叉查找树 ,红黑树, 算法:理解红黑树 (英文pdf:红黑树) 目录 自平衡二叉树介绍 avl树 2-3树 LLRBT(Left-leaning red-black tree左倾红黑树 ...

  8. JavaScript入门学习之二——函数

    在前一章中讲了JavaScript的入门语法,在这一章要看看函数的使用. 函数的定义 JavaScript中的函数和Python中的非常类似,只不过定义的方式有些不同,下面看看是怎么定义的 //定义普 ...

  9. 使用SetWindowHookEx注入global hook

    写下这是为了自己复习的. 主要实现的是给File Explorer注入鼠标钩子,以检测鼠标是否在File Explorer上点击 .cpp #include <Windows.h> #in ...

  10. ios 打包下

    一.打包真机方式 二.编译打包 三.配置打包信息 以下为打的包: