Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same,
where in each step you can delete one character in either string.
Example 1:
Input: "sea", "eat"
Output: 2
Explanation: You need one step to make "sea" to "ea" and another step to make "eat" to "ea".
Note:
The length of given words won't exceed 500.
Characters in given words can only be lower-case letters.

思路:

首先求最长公共子序列(LCS),然后,用两个字符串的长度分别减去公共子序列的长度,然后再相加即为要删除的长度。

最长公共子序列是经典的动态规划问题。

最长公共子序列问题存在最优子结构:这个问题可以分解成更小,更简单的“子问题”,这个子问题可以分成更多的子问题,因此整个问题就变得简单了。最长公共子序列问题的子问题的解是可以重复使用的,也就是说,更高级别的子问题通常会重用低级子问题的解。拥有这个两个属性的问题可以使用动态规划算法来解决,这样子问题的解就可以被储存起来,而不用重复计算。这个过程需要在一个表中储存同一级别的子问题的解,因此这个解可以被更高级的子问题使用。

动态规划的一个计算最长公共子序列的方法如下,以两个序列{\displaystyle X}、{\displaystyle Y}为例子:

设有二维数组表示位和位之前的最长公共子序列的长度,则有:

其中,的第位与的第位完全相同时为“1”,否则为“0”。

此时,中最大的数便是的最长公共子序列的长度,依据该数组回溯,便可找出最长公共子序列。

 int minDistance2(string word1, string word2)
{
vector<vector<int>> dp(word1.size()+,vector<int>(word2.size()+,));
for (int i = ; i <= word1.size();i++)
{
for (int j = ; j <= word2.size();j++)
{
if (i == || j == ) dp[i][j] = ;
else if (word1[i-] == word2[j-])
{
dp[i][j] = dp[i - ][j - ] + ;
}
else
{
dp[i][j] = max(dp[i - ][j], dp[i][j - ]);
} }
}
int lcs = dp[word1.size()][word2.size()];
return word1.size() - lcs + word2.size() - lcs;
}

参考:

http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence/

https://zh.wikipedia.org/wiki/%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97

https://discuss.leetcode.com/topic/89285/java-dp-solution-longest-common-subsequence

[leetcode-583-Delete Operation for Two Strings]的更多相关文章

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

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

  3. 【Leetcode】583. Delete Operation for Two Strings

    583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...

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

  5. 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)

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

  6. 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 Delete Operation for Two Strings

    原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目: Given two wo ...

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

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

  9. [Swift]LeetCode583. 两个字符串的删除操作 | Delete Operation for Two Strings

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

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

随机推荐

  1. Arcengine 二次开发添加右键菜单

    最近在搞arcengine 二次开发,遇到了好多问题,也通过网上查资料试着慢慢解决了,把解决的步骤记录下来,有需要帮助的可以看一下,也欢迎各位来批评指正. 想给自己的map application在图 ...

  2. Java 7 Fork/Join 框架

    在 Java7引入的诸多新特性中,Fork/Join 框架无疑是重要的一项.JSR166旨在标准化一个实质上可扩展的框架,以将并行计算的通用工具类组织成一个类似java.util中Collection ...

  3. 技术福利:mysql数据库的基本命令汇总整理

    一.创建数据库: create database database_name: php中创建数据库的两种方法:(mysql_create_db(),mysql_query()) $conn = mys ...

  4. ztree实现权限功能(横向显示)

    最近在做权限功能的时候,采用的ztree实现的,但是产品要求最后一层的权限节点要横向显示.开始在网上找的解决方案是用css样式把最后一层的display设置为inline.在我本地电脑上看了下.效果不 ...

  5. MacBook使用之配置jdk&Eclipse

    查看系统版本:关于本机-软件-查看当前版本信息 打开另一个Finder的快捷键:Command + n 终端命令:Finder - 使用工具 - 终端命令 配置jdk系统变量: cd ~ touch ...

  6. JVM垃圾收集算法

    JVM垃圾收集 1. 判断对象是否存活 引用计数算法 对象添加一个引用计数器,每个地方引用它,计数器值加+1:当引用失效,计算器值减1:任何时刻计数器为0的对象不可能被使用.引用计数算法实现简单,高效 ...

  7. 忘记block格式 xib加载没有计算导航栏和tabbar的大小

    敲inlineBlock xib加载没有计算导航栏和tabbar的大小 /将这个属性改为no self.tabBarController.tabBar.translucent = NO; 判断优化,两 ...

  8. #415 Div2 C

    #415 Div2 C 题意 给定一个数字集合,找到所有子集合最大值与最小值之差的和. 分析 列式子,找规律. $ (a_2 - a_1) * 2^0 + (a_3 - a_1) * 2^1 + .. ...

  9. 是时候开始用C#快速开发移动应用了

    从2015年接触Xamarin到至今已经2个年头,我对Xamarin的技能没有长进多少,但它却已经足够成熟到在跨平台移动开发工具中占有一席之地.在扫了一些资料之后,突然发现国外有很多移动端的应用已经是 ...

  10. java(3) if结构

    一.基本if结构 1.流程图 1)输入输出 2)判断和分支 3) 流程线 1.1  简单的if条件判断 if(表达式){             //表达式为true,执行{}中的代码 } 示例1:如 ...