leetcode72. Edit Distance(编辑距离)
- 以下为个人翻译方便理解
- 编辑距离问题是一个经典的动态规划问题。首先定义dp[i][j表示word1[0..i-1]到word2[0..j-1]的最小操作数(即编辑距离)。
- 状态转换方程有两种情况:边界情况和一般情况,以上表示中 i和j均从1开始(注释:即至少一个字符的字符串向一个字符的字符串转换,0字符到0字符转换编辑距离自然为0)
- 1.边界情况:将一个字符串转化为空串,很容易看出把word[0...i-1]转化成空串“”至少需要i次操作(注释:i次删除),则其编辑距离为i,即:dp[i][0] = i;dp[0][j] = j.
2.一般情况:转化非空字符串word1[0..i - 1] 为另一非空字符串 word2[0..j - 1],此处问题转化为几个个子问题:假定已知word1[0..i - 2] 到 word2[0..j - 2]的编辑距离, 即dp[i - 1][j - 1],只需考虑word[i - 1] 和 word2[j - 1]
- 如果 word[i - 1] == word2[j - 1],无需操作即可满足word1[0..i - 1] 与 word2[0..j - 1]相同,则编辑距离dp[i][j] = dp[i - 1][j - 1]
- 如果 word[i - 1] != word2[j - 1],分为三种子情况:
- 用 word2[j - 1]替换word1[i - 1],则有 (dp[i][j] = dp[i - 1][j - 1] + 1 (一次操作用于替换));
- 删除 word1[i - 1] 使得 word1[0..i - 2] = word2[0..j - 1],则有(dp[i][j] = dp[i - 1][j] + 1 (一次操作用于删除));
- 在word1[0..i - 1] 中插入 word2[j - 1] 使得 word1[0..i - 1] + word2[j - 1] = word2[0..j - 1] ,则有(dp[i][j] = dp[i][j - 1] + 1 (一次操作用于插入)).
为了保证理解插入和删除带来的细微差别,对于删除,其实是将word1[0..i - 2] 转化成 word2[0..j - 1], 编辑距离是 dp[i - 1][j],之后直接删除word1[i - 1],一次操作,插入也是类似
(注释:就是由word1[0..i - 2] 编辑转化成 word2[0..j - 1],删除word1[i - 1]两个操作共同完成实现将word1[0..i - 1] 转化成 word2[0..j - 1])- 合并规如下:
- dp[i][0] = i;
- dp[0][j] = j;
- dp[i][j] = dp[i - 1][j - 1], if word1[i - 1] = word2[j - 1];
- dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i - 1][j] + 1, dp[i][j - 1] + 1)
- 转化为代码如下
'''
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
vector - 你可能会注意到每次更新dp[i][j],我们只需要dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]就行
- 事实上我们不必维护整个m*n矩阵。相反维护一栏即可,代码空间复杂度降为O(m)或者O(n)取决于你维护的的是矩阵的一行还是一列
优化后代码如下:
'''
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.length(), n = word2.length();
vector
leetcode72. Edit Distance(编辑距离)的更多相关文章
- Edit Distance编辑距离(NM tag)- sam/bam格式解读进阶
sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过b ...
- [LeetCode] Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- leetCode 72.Edit Distance (编辑距离) 解题思路和方法
Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert ...
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [leetcode72]Edit Distance(dp)
题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...
- leetcode72. Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【LeetCode每天一题】Edit Distance(编辑距离)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- edit distance(编辑距离,两个字符串之间相似性的问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
随机推荐
- JS在路径中传中文参数
需要用 encodeURI('中文');处理一下.
- Python 类与作用域——一些测试
/* 一 */ >>> class T (): a = 0 b = a >>> T.b 0 >>> del T /* 二 */ >>& ...
- Web 应用程序中的安全向量 – ASP.NET MVC 4 系列
Web 程序运行在标准的.基于文本的协议(HTTP 和 HTML)之上,所以特别容易受到自动攻击的伤害.本章主要介绍黑客如何滥用应用程序,以及针对这些问题的应对措施. 威胁:跨站脚本 ...
- jQuery实例——jQuery实现联动下拉列表查询框--转http://www.cnblogs.com/picaso/archive/2012/04/08/2437442.html#undefined
jQuery实例--jQuery实现联动下拉列表查询框 在查询与列表显示的时候经常用到联动列表显示,比如一级选项是国家,二级选项是省,三级是市,这样的联动是联系的实时导出的,比如你不可能选择了四川 ...
- WebService 不依赖配置文件直接在构造函数配置地址
研究了下 ClientBase(Binding binding, EndpointAddress remoteAddress) 这个重载更好用,都不用填名称比如 new PAS.WebService. ...
- bash shell,调用ffmpeg定期截图
#!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(十二) 代码重构使用反射工厂解耦(一)缓存切换
前言 上一篇中,我们用了反射工厂来解除BLL和UI层耦合的问题.当然那是最简单的解决方法,再复杂一点的程序可能思路相同,但是在编程细节中需要考虑的就更多了,比如今天我在重构过程中遇到的问题.也是接下来 ...
- DevWebForm控件,触发用客户端事件
ClientInstanceName+".Raise"+"事件名", 例如: ClientInstanceName.RaiseValueChanged();
- mfc打开程序
void CMy3MFCDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 HINSTANCE hRslt = ShellExecute(NULL, ...
- haoop 断电后导致block文件损坏
hbase将dfs作为存储,公司测试环境断电后,hadoop集群会因此而损坏一些block文件,这个时候,客户端在读取文件时会报一些错: DataXceiver error processing RE ...