题目:

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

思路:

这是一道典型的动态规划题目,建立二维数组dp,其中dp[i][j]表示字符串s1的前i个字符和字符串s2的前j个字符要变相等所需要删除的字符的最小ASCII码。

首先对dp进行初始化,当一个字符串为空时,另一个字符串需要删除全部的字符串才能保证两个字符串相等。

for (int i = ; i <= (signed) s1.length(); i++) {
dp[i][] = dp[i - ][] + (int) s1[i - ];
}
for (int i = ; i <= (signed) s2.length(); i++)
dp[][i] = dp[][i - ] + (int) s2[i - ];

对于dp[i][j],一共有3三种方法到达dp[i][j]

当s1[i-1] == s2[j-1]时,不需要删除s1[i-1]和s2[j-1]就可以保证两个字符串相等,此时dp[i][j] = dp[i-1][j-1]

当s1[i-1] != s2[j-1]时,dp[i][j]可以等于dp[i-1][j]+s1[i-1],也可以等于dp[i][j-1]+s2[j-1]。

  dp[i-1][j]+s1[i-1]表示由于从dp[i-1][j]到dp[i][j],增加了字符s1[i-1],s2的字符没有变。想要相同,就必须删除s1[i-1]。

  dp[i][j-1]+s2[j-1]表示由于从dp[i][j-1]到dp[i][j],增加了字符s2[j-1],s1的字符没有变。想要相同,就必须删除s2[j-1]。

代码:

 #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
//dp[i][j]代表s1的前i个字符与s2的前j个字符相等所需要删除的最小ascii码
int dp[s1.length() + ][s2.length() + ] = { };
//初始化
for (int i = ; i <= (signed) s1.length(); i++) {
dp[i][] = dp[i - ][] + (int) s1[i - ];
}
for (int i = ; i <= (signed) s2.length(); i++)
dp[][i] = dp[][i - ] + (int) s2[i - ]; for (int i = ; i <= (signed) s1.length(); i++) {
for (int j = ; j <= (signed) s2.length(); j++) {
//字符相等,不需要删除元素
if (s1[i - ] == s2[j - ])
dp[i][j] = dp[i - ][j - ];
else {
dp[i][j] = min(dp[i - ][j] + (int) s1[i - ],
dp[i][j - ] + (int) s2[j - ]);
}
cout << dp[i][j] << " ";
}
}
cout << endl;
return dp[s1.length()][s2.length()];
}
};

举例:

以sea和eat为例。

当i=1,j=1时,dp[1][1] = min(dp[0][1]+s1[0],dp[1][0]+s2[0])  = e+s = 216

  dp[0][1]+s1[0]表示eat已经删除了e,sea需要删除s。

  dp[1][0]+s2[0]表示sea已经删除了s,eat需要删除e。

当i=1,j=2时,dp[1][2] = min(dp[0][2]+s2[0],dp[1][1]+s2[1]) = min(ea+s,se+a) = a+e+s = 313

  dp[0][2]+s2[0]表示eat已经删除了ea,sea需要删除s。

  dp[1][1]+s2[1]表示sea已经删除了s,eat已经删除了e,需要删除a。

当i=1,j=3时,dp[1][3] = min(dp[0][3]+s1[0],dp[1][2]+s2[2]) = min(eat+s,s+ea+t) = a+e+s+t = 429

  dp[0][3]+s1[0]表示eat已经删了eat,sea需要删除s。

  dp[1][2]+s2[2]表示sea已经删除了s,eat已经删除了ea,需要删除t。

当i=2,j=1时,dp[2][1] = dp[1][0] = 115

  由于s1[1]与s2[0]相同,只需要删除sea中的s。

当i = 2,j = 2时,dp[2][2] = min(dp[1][2]+s1[1],dp[2][1]+s2[1]) = a+s = 212

  dp[1][2]+s1[1]表示sea已经删除了s,eat已经删除了ea,sea还需要删除e。

  dp[2][1]+s2[1]表示sea已经删除了s,eat还需要删除a。

当i=2,j=3时,dp[2][3] = min(dp[1][3]+s1[1],dp[2][2]+s2[2]) = a+s+t = 328

  dp[1][3]+s1[1]表示sea已经删除了s,eat已经删除了eat,sea还需要删除e。

  dp[2][2]+s2[2]表示sea已经删除了s,eat已经删除了a,eat还需要删除t。

当i=3,j=1时,dp[3][1] = min(dp[2][1]+s1[2],dp[3][0]+s2[0]) = s+a = 212

  dp[2][1]+s1[2]表示sea已经删除了s,sea还需要删除a。

  dp[3][0]+s2[0]表示sea已经删除了sea,eat还需要删除a。

当i=3,j=2时,dp[3][2] = dp[2][1] = 115

当i=3,j=3时,dp[3][3] = min(dp[2][3]+s1[2],dp[3][2]+s2[2]) = s + t = 231

  dp[3][2]+s2[2]表示sea已经删除了s,eat还需要删除t。

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

  3. [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. ...

  4. 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)

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

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

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

  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. CentOS 7 zabbix添加监控服务器

    CentOS 7 yum安装zabbix 设置中文界面 安装环境 CentOS 7  关闭防火墙和SElinux 在被监控端安装zabbix-agent [root@zabbix-agent ~]# ...

  2. WCF基础二

    1:地址 . WCf 的每一个服务都有一个唯一地址,地址包括传输协议和服务位置. HTTP:采用http\https协议进行传输,默认端口为80. TCP:采用ne.tcp协议进行传输,默认端口为80 ...

  3. CCF CSP 201712-1 最小差值

    题目链接:http://118.190.20.162/view.page?gpid=T68 问题描述 试题编号: 201712-1 试题名称: 最小差值 时间限制: 1.0s 内存限制: 256.0M ...

  4. js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结

    写前端的时候,很多的时候是避免不了注册这一关的,但是一般的注册是没有任何的难度的,无非就是一些简单的获取用户输入的数据,然后进行简单的校验以后调用接口,将数据发送到后端,完成一个简单的注册的流程,那么 ...

  5. 关于 IIS 的 Management Service Delegation 配置 备份

    在MSDN没找到关于使用APPCMD备份IIS的"Management Service Delegation"模块配置命令. 到IIS的配置文件存放目录下,几番搜索,查到%wind ...

  6. jquery的优良继承方法

    说一下好处:这个封装函数可以可以实现子类继承父类原型对象里面的所有方法和属性,但是也留了第二条路,去继承父类构造函数的里面的东西. 两个参数分别是子类的构造函数,后面是父类构造函数 $.inherit ...

  7. Django路由控制

    本文目录 一 Django中路由的作用 二 简单的路由配置 三 有名分组 四 路由分发 五 反向解析 六 名称空间 七 django2.0版的path 回到目录 一 Django中路由的作用 URL配 ...

  8. 自动化运维 --- git

    一. git 概括 二.常用指令 git init 初始化 git status 查看git的状态 git add 将文件放到缓存区 git commit -m 将缓存区的内容提交到本地仓库 git ...

  9. P4389 付公主的背包

    注意 初始化的时候要这样写 for(int i=1,x;i<=n;i++){ scanf("%d",&x); v[x]++; } for(int i=1;i<= ...

  10. Java集合源码分析之ArrayList

    ArrayList简介 从上图可以看到,ArrayList是集合框架中List接口的一个实现类,它继承了AbstractList类,实现了List, RandomAccess, Cloneable, ...