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服务无法正常启动问题

    使用mysql的时候,突然查看服务列表也找不到mysql服务 解决办法: 一.首先打开CMD,切换到MySql安装目录的MySql Server →bin目录下 运行如下命令(具体试个人安装的MySq ...

  2. 接口测试工具中 post请求如何传递多维数组

    1,请求参数为数组时,可以采用传递 json格式的形式传递请求参数(字段及字段对应的值如查是字符,都应该用双引号括起来.用单引号会无法识别),后台接收的数据为json . 2,直接以数组格式来请请求 ...

  3. CNN for NLP

    卷积神经网络在自然语言处理任务中的应用.参考链接:Understanding Convolutional Neural Networks for NLP(2015.11) Instead of ima ...

  4. Python学习 第一天(一)初始python

    1.python的前世今生 想要充分的了解一个人,无外乎首先充分了解他的过去和现在:咱们学习语言也是一样的套路 1.1 python的历史 Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈp ...

  5. 使用powershell的remove

    使用Remove-Module -Name Functions2 只是移除当前会话的模块 并且Import-Module -Name Functions2与 Remove-Module -Name F ...

  6. 利用jQuery实现图片无限循环轮播(不借助于轮播插件)

    原来我主要是用Bootstrap框架或者swiper插件实现轮播图的功能,而这次是用jQuery来实现图片无限循环轮播! 用到的技术有:html.css.JavaScript(少).jQuery(主要 ...

  7. javaScript--for循环的性能提升

    //为什么性能提升? //第一种写法每次循环时都需要从内存中取出students.length并判断条件是否成立 //第二种写法只需要取一次students.length就可以 //第二种方式弊端:l ...

  8. MongoDB——增删改查

    文档结构: { "_id": ObjectId("5d5e5de597eb2f0b70005d1a"), , "word_records": ...

  9. HTML5测试题整理Ⅰ

    1.在 HTML5 中,哪个元素用于组合标题元素? 答案:<hgroup>   2.HTML5 中不再支持哪个元素? 答案:<font>,<acronym>,< ...

  10. jquery submit选择器 语法

    jquery submit选择器 语法 作用::submit 选择器选取类型为 submit 的 <button> 和 <input> 元素.如果 <button> ...