[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. ...
随机推荐
- Node.js编写CLI的实践
导语:通常而言,Node.js的应用场景有前后端分离.海量web页面渲染服务.命令行工具和桌面端应用等等.本篇文章选取CLI(Command Line Tools)这子领域,来谈谈Node.js编写C ...
- OOP 三大特点:继承性,封装性,多态性
1.继承性:代码重用 2.封装性: 使相似数据和操作进行封装,保持代码安全 3.多态性: PHP不支持多态
- React入门---JSX内置表达式-6
个人理解:接触的JSX就是在React中render方法里面的js,因为里面只能有一个节点,所以你写的东西都在一个div中,要有js所以通过JSX来表达.(个人菜鸟理解,欢迎指正) React 使用 ...
- Vue中comoputed中的数据绑定
Vue中的数据实现响应式绑定是在初始化的时候利用definePrototype的定义set和get过滤器,在进行组件模板编译时实现water的监听搜集依赖项,当数据发生变化时在set中通过调用dep. ...
- 【http】http的方法,状态码和组成部分
Http(Hypertext Transfer Protocol) HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传 ...
- Jedis-returnResource使用注意事项
遇到过这样一个严重问题: 发布的项目不知从什么时候开始,每月会出现一两次串号问题.串号现象指的是,用户用账号A登录系统,然后某个时间,登录账号自动变成了B. 串号出现的时间不定,测试平台难以重现,且后 ...
- 使用CSS设置滚动条样式以及如何去掉滚动条的方法
<STYLE> BODY { SCROLLBAR-FACE-COLOR: #f892cc; SCROLLBAR-HIGHLIGHT-COLOR: #f256c6; SCROLLBAR-SH ...
- Linux上open-iscsi 的安装,配置和使用
关于open-iscsi open-iscsi是一个实现 RFC3720 iSCSI协议的高性能initiator程序.iSCSI使得访问SAN上的存储不再只能依赖Fibre Channel,也可以通 ...
- Java中的会话管理——HttpServlet,Cookies,URL Rewriting(译)
参考谷歌翻译,关键字直接使用英文,原文地址:http://www.journaldev.com/1907/java-session-management-servlet-httpsession-url ...
- 禁止右键,禁止选中,禁止网页复制的Js代码
document.oncontextmenu=new Function('event.returnValue=false;'); document.onselectstart=new Function ...