[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. ...
随机推荐
- 最近一些朋友问我,临近快毕业了专业不对口,想转行看到IT行业就业前景不错,但是编程语言众多不了解,不知道哪门语言能够快速入门掌握,短期能让我找到工作
我做互联网前端后台开发也有四年多了,一路走过来,累并快乐着.快乐比艰辛更多,源自我的兴趣驱动.初中的一个偶然的机会我接触到了计算机,从那个时候就喜欢上开始经常到网吧上网.那个时候我对计算机领域的认识是 ...
- 用ajax写分页查询-----2017-05-17
要写分页,首先你得清楚,一页你想显示多少条信息?如何计算总共显示的页数? 先说一下思路: (1)从数据库读取数据,以chenai表为例,读取所有留言信息.并能够实现输入发送者,可以查询该发送者的留言总 ...
- JEESZ-Zookeeper集群安装
1. 在根目录创建zookeeper文件夹(service1.service2.service3都创建) [root@localhost /]# mkdir zookeeper 通过Xshell上传文 ...
- 8、单选按钮(JRadioButton)和复选框(JCheckBox)
8.单选按钮(JRadioButton)和复选框(JCheckBox) 实现一个单选按钮(或复选框),此按钮项可被选择或取消选择,并显示其状态.JRadioButton对象与ButtonGroup对象 ...
- 2、Java应用中常见的JDBC连接字符串(SQLite、MySQL、Oracle、Sybase、SQLServer、DB2)
2.Java应用中常见的JDBC连接字符串 Java应用中连接数据库是不可或缺的,于是便整理一些可能用到的JDBC的jar包及其相匹配的URL,以备日后查阅. 1)SQLite Class.forNa ...
- 200 OK (from cache)原因
Meta标签中的http-equiv用来标记不可缓存或过期时间,但效果一般.而且代理缓存基本不访问HTML文档内容,所以尽量少用meta标签控制缓存. Pragma: no-cache Forces ...
- jar包和war包
Jar (Java archive), 是将实现了某功能的所有类及辅助资源用ZIP压缩形式打包而成的一个文件, 便于代码的管理和重复使用.当使用别人提供的jar时,只需要在classpath环境变量中 ...
- 面试(4)-spring-Spring面试题和答案
1:69道Spring面试题和答案 转自:http://ifeve.com/spring-interview-questions-and-answers/ 目录 Spring 概述 依赖注入 Spri ...
- 由Find All References引发的思考。,
今天在研究C#代码问题的时候遇到了一个Visual Studio的小问题.在Visual Studio 2013中,使用Find All References功能不能找到同一类型不同版本的所有引用,具 ...
- C# Ajax 返回json数据--前后台交互
本人实习生一枚,遇到这个问题,网上找的试了试基本可以,自己搞了一下.可以供新手参考,大神如有指点,请不吝赐教. 版权声明:本文为博主原创文章,未经博主允许不得转载. 前台JavaScript代码: & ...