Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)

You have the following 3 operations permitted on a word:

a) Insert a character

b) Delete a character

c) Replace a character

别人的思路:

自然语言处理(NLP)中。有一个基本问题就是求两个字符串的minimal Edit Distance, 也称Levenshtein distance。受到一篇Edit Distance介绍文章的启示。本文用动态规划求取了两个字符串之间的minimal Edit Distance. 动态规划方程将在下文进行解说。

1. what is minimal edit distance?

简单地说。就是仅通过插入(insert)、删除(delete)和替换(substitute)个操作将一个字符串s1变换到还有一个字符串s2的最少步骤数。熟悉算法的同学非常easy知道这是个动态规划问题。
事实上一个替换操作能够相当于一个delete+一个insert,所以我们将权值定义例如以下:
I (insert):1
D (delete):1
S (substitute):2 2. example:
intention->execution
Minimal edit distance:
delete i ; n->e ; t->x ; insert c ; n->u 求和得cost=8 3.calculate minimal edit distance dynamically
思路见凝视,这里D[i,j]就是取s1前i个character和s2前j个character所得minimal edit distance
三个操作动态进行更新:
D(i,j)=min { D(i-1, j) +1, D(i, j-1) +1 , D(i-1, j-1) + s1[i]==s2[j] ? 0 : 2}。中的三项分别相应D,I,S。(详见我同学的博客)

由于本题的替换操作权重相同为1。故字符不相等+1就可以。

代码例如以下:

public class Solution {
public int minDistance(String word1, String word2) {
//边界条件
if(word1.length() == 0)
return word2.length();
if(word2.length() == 0)
return word1.length();
/*
* 本题用动态规划的解法
* f[i][j]表示word1的前i个单词到word2前j个单词的最短距离
* 状态转移方程:f[i][j] =
*/ int[][] f = new int[word1.length()][word2.length()];
boolean isEquals = false;//是否已经有相等
for(int i = 0 ; i < word2.length(); i++){
//假设相等,则距离不添加
if(word1.charAt(0) == word2.charAt(i) && !isEquals){
f[0][i] = i > 0 ? f[0][i-1]:0;//不能从0開始
isEquals = true;
}else{
f[0][i] = i > 0 ? f[0][i-1]+1:1;
}
}
isEquals = false;//是否已经有相等
for(int i = 1 ; i < word1.length(); i++){
//假设相等,则距离不添加
if(word1.charAt(i) == word2.charAt(0) && !isEquals){
f[i][0] = f[i-1][0];//不能从0開始
isEquals = true;
}else{
f[i][0] = f[i-1][0]+1;
}
} for(int i = 1; i < word1.length();i++){
for(int j = 1; j < word2.length(); j++){
if(word1.charAt(i) == word2.charAt(j)){
f[i][j] = f[i-1][j-1];//相等的话直接相等
}else{
f[i][j] = f[i-1][j-1]+1;
}
//然后与从f[i-1][j]+1。f[i][j-1]+1比較,取最小值
f[i][j] = Math.min(f[i][j],Math.min(f[i-1][j]+1,f[i][j-1]+1));
}
}
return f[word1.length()-1][word2.length()-1];
}
}

leetCode 72.Edit Distance (编辑距离) 解题思路和方法的更多相关文章

  1. [LeetCode] 72. Edit Distance 编辑距离

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  2. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  3. 【LeetCode】72. Edit Distance 编辑距离(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...

  4. LeetCode - 72. Edit Distance

    最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...

  5. [leetcode]72. Edit Distance 最少编辑步数

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...

  6. 72. Edit Distance(编辑距离 动态规划)

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  7. 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP

    Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...

  8. [leetcode] 72. Edit Distance (hard)

    原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...

  9. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

随机推荐

  1. JAVA程序设计(11)-----面对对象0基础设计 麻将 创建麻将牌 然后洗牌 发牌~ 恩 就这样

    zzzzZZZZ 1.開始还想贴图的 实在太懒了-- 这是一张麻将 package com.lovo; import java.awt.Graphics; import java.awt.Image; ...

  2. 用jquery实现隐藏列表表单的显示关闭切换以及Ajax方式改动提交相应的那一行的改动内容。

    请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 先给大家看看图片效果,大 ...

  3. 《学习opencv》笔记——基本数据结构,CvMat,矩阵訪问

        老板让让做一个东东.输入端要用到opencv顺便就来学习一下.买了本书<学习opencv>翻来一看,opencv1.0,去官网上一看.opencv2.49,瞬间有种蛋碎的赶脚.看着 ...

  4. NAS配置Time Machine,在D-Link DNS-320上的配置笔记

    今天打算把Time Machine备份的工作交给NAS,曾经是放在一块外置硬盘上的,尽管速度要比NAS快,可是每次插拔外接都有些麻烦.而NAS又具有实时在线.定时关机启动的功能.配合Time Mach ...

  5. 个人作业—Alpha项目测试

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...

  6. $.ajax 和$.post的区别

    https://blog.csdn.net/weixin_39709686/article/details/78680754

  7. 获取sqlserver数据字典的完整sql

    SELECTsysobjects.name AS 表名称 , --------------as 的作用:为字段起一个别名 --sysproperties.[value] AS 表说明 , ------ ...

  8. JS进阶 - 浏览器工作原理

    一.浏览器的结构 浏览器的主要组件为: 用户界面 - 包括地址栏.前进/后退按钮.书签菜单等.除了浏览器主窗口(显示页面),其他部分都属于用户界面. 浏览器引擎 - 在用户界面和渲染引擎之间传送指令. ...

  9. CentOS 安装 PHP 扩展

    下载地址:https://pecl.php.net/package/redis 上传目录:/usr/local/src //安装依赖 yum install php-devel -y //进入安装包目 ...

  10. gdal集成kml库的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近要读取kml文件,具体就是把kml文件当作一个矢量文件来读取.我发现gdal是支持集成kml库的.不过集成这个km ...