[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 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]的更多相关文章
- [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 ...
- 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 ...
- 【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 ...
- 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 ...
- 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- LeetCode Delete Operation for Two Strings
原题链接在这里:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目: Given two wo ...
- [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 ...
- [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 ...
- [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. ...
随机推荐
- JAVA的Executor框架
Executor框架分离了任务的创建和执行.JAVA SE5的java.util.concurrent包中的执行器(Executor)管理Thread对象,从而简化了并发编程.Executor引入了一 ...
- CI Weekly #19 | 关于软件开发模型的思考,以及最新 CI/CD 实践分享
五月一来,夏天便悄然而至.flow.ci 也带来了几个新的变化,帮你进一步优化开发工作流.一起来看看这几个重点功能: 支持 iOS 项目 Xcode8.3 构建 iOSer 们重点来了,flow.ci ...
- Oracle 12C 新特性之 PDB热克隆(本地克隆、远端异机克隆)
说明:版本12.2.0.1 12c r1版本中 clone 一份PDB源库需要打开在read only只读模式 , 在12c r2版本中引入了local undo mode, 源PDB在read/wr ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- Eclipse设置问题:字体大小、修改注释内容、修改快捷键
一.设置字体大小,看下图,包括了设计代码字体大小和控制台输出字体大小 二.修改注释内容 选择window---->>preferences 选择Java---->>code s ...
- css相关tips
12px的中文占据16px高度,英文占据14px的高度.所以做双语版网页时css样式要做相应调整. IE10,IE11浏览器当点击input text文本框时,输入文本后出现一个删除功能的X按钮. 去 ...
- 五、 创建连接串连接本地数据库(ASP.NET MVC5 系列)
1. 创建连接串连接本地SQLServer数据库 上节讲到MovieDBContext类,这个类的作用是连接数据库并将Movie对象迁移到数据库记录中.不过你会问一个问题:如何知道这个对象将连接哪个数 ...
- An Introduction to Stock Market Data Analysis with R (Part 1)
Around September of 2016 I wrote two articles on using Python for accessing, visualizing, and evalua ...
- spring 事务无效解决方法
(原) spring 事务目前有二种,注解式和声明式,以前都是以公司里的框架写好的,没有学习的机会,今天抽空好好试了下,结果遇到好多问题. 1.注解式 最开始是这么玩的,发现数据进数据库了,没有起作用 ...
- 两种最常用的Sticky footer布局方式
Sticky footer布局是什么? 我们所见到的大部分网站页面,都会把一个页面分为头部区块.内容区块和页脚区块,当头部区块和内容区块内容较少时,页脚能固定在屏幕的底部,而非随着文档流排布.当页面内 ...