这道题的思路:我是根据最长公共子序列的思路得来的。

最长公共子序列是: d[i][j]表示字符串s1前i个(0~i-1)字符,和字符串s2前j个(0~j-1)字符的最长公共子序列。

分情况讨论:

  当s1[i-1] == s2[j-1]的时候,d[i][j] = d[i-1][j-1]+1; 这个表示 ,当第i-1个字符相同时,就在之前d[i-1][j-1]的长度上加一

当s1[i-1] != s2[j-1]的时候,d[i][j] 的值就有可能有两种情况:

(1)d[i][j] = d[i-1][j],表示d[i][j]这个最长公共子序列,没有字符串s1[i-1]。那么就是相当于字符串s1(0~i-2)和字符串s2(0~j-1)的最长公共子序列

(2)d[i][j] = d[i][j-1],  表示d[i][j]这个最长公共子序列,没有字符串s2[i-1]。那么就是相当于字符串s1(0~i-1)和字符串s2(0~j-2)的最长公共子序列

综上,d[i][j] = max(d[i-1][j],d[i][j-1])

根据上面的这种思路,思考712这道题,这道题的意思是,删除字符,使得两个字符串相等,删除的字符相加起来,使得删除字符串的和最小。

可以设 d[i][i] 表示字符串s1前i个(0~i-1)字符和字符串s2前j个(0~j-1)字符保持相等的删除字符最小和

那么对于d[i][j] 也是分情况讨论:

当s1[i-1] ==s2[j-1] , 这就表示不需要删除s1[i-1]和s2[j-1],则d[i][j] = d[i-1][j-1]

当s1[i-1]!=s2[j-1],  两个字符都不要 ,则d[i][j] = d[i-1][j-1]+s1[i-1]+s2[j-1]

再讨论,无论s1[i-1]和s2[j-1]是否相等,

(1)不要s1[i-1],在s1[0~i-2]和 s2[0~j-1]中找删除字符串最小和

(2)不要s2[j-1], 在s1[0~i-1]和s2[0~j-2]中找删除字符串最小和

代码如下:

class Solution {
public:
int minimumDeleteSum(string s1, string s2) {
int m = s1.length();
int n =s2.length();
int d[m+][n+];
d[][] = ;
for(int i =;i<=m;i++)
d[i][] = d[i-][]+s1[i-];
for(int j =;j<=n;j++)
d[][j] = d[][j-]+s2[j-];
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
d[i][j] = min(d[i-][j]+s1[i-],d[i][j-]+s2[j-]);
if(s1[i-]==s2[j-])
d[i][j] = min(d[i][j],d[i-][j-]);
else
d[i][j] - min(d[i][j],d[i-][j-]+s1[i-]+s2[j-]);
}
return d[m][n];
}
};

leetcode 712的更多相关文章

  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. Java实现 LeetCode 712 两个字符串的最小ASCII删除和(最长公共子串&&ASCII值最小)

    712. 两个字符串的最小ASCII删除和 给定两个字符串s1, s2,找到使两个字符串相等所需删除字符的ASCII值的最小和. 示例 1: 输入: s1 = "sea", s2 ...

  4. Leetcode 712. 两个字符串的最小ASCII删除和

    题目描述: https://leetcode-cn.com/problems/minimum-ascii-delete-sum-for-two-strings/ 解题思路: 也是典型的dp问题.利用二 ...

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

  6. [LeetCode] 583. Delete Operation for Two Strings 两个字符串的删除操作

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  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】712. Minimum ASCII Delete Sum for Two Strings

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

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

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

随机推荐

  1. 测试PHP-FPM的工作流中的疑惑点

    顺序比较乱,想到什么测试什么,测试环境 PHP7.2 和 MariaDB10.3.11 PHP-FPM是 master/worker 多进程模型master负责和web-server通讯,把接受到请求 ...

  2. docker-compose命令使用说明

    Commands: build Build or rebuild services bundle Generate a Docker bundle from the Compose file conf ...

  3. backpropagation algorithm

    搞卷积神经网络的时候突然发现自己不清楚神经网络怎么训练了,满脸黑线,借此机会复习一下把. 首先放一位知乎大佬的解释.https://www.zhihu.com/question/27239198?rf ...

  4. pytest用例传参的多种方式

    1.接收外部传参 *函数获取需要的参数,再传入 *函数获登录信息,直接使用 2.其它方式传参 *依据dict取值 *tuple数组

  5. js --策略模式

    策略模式的定义: 将算法一个个的单独进行封装,并且使他们可以相互替换.此模式让算法的变化不会影响到使用算法的客户. 先回顾一下,我们在做项目的过程中,是不是经常会遇见因为业务逻辑的关系,我们会写好多的 ...

  6. GitHub上传文件问题总结

    问题一:git warning: LF will be replaced by CRLF in 解决办法 在Git Bash中输入git add .时出现上述语句. 解决办法: 输入以下语句: $ g ...

  7. MySQL5.7.16安装及配置

    一.下载 下载页面http://dev.mysql.com/downloads/mysql/ 选择系统平台后,点击download(根据系统选择64或32位) 二.配置 1.下载成功后,解压安装包到要 ...

  8. day26-python之封装

    1.动态导入模块 # module_t=__import__('m1.t') # print(module_t) # module_t = __import__('m1.t') # print(mod ...

  9. IIS-This configuration section cannot be used at this path.

    Q:在IIS上部署web后,游览器打开报以下异常: This configuration section cannot be used at this path. This happens when ...

  10. [堆栈]Linux 中的各种栈:进程栈 线程栈 内核栈 中断栈

    转自:https://blog.csdn.net/yangkuanqaz85988/article/details/52403726 问题1:不同线程/进程拥有着不同的栈,那系统所有的中断用的是同一个 ...