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. 16进制颜色字符串转为UIColor

    //16进制颜色(html颜色值)字符串转为UIColor +(UIColor *) hexStringToColor: (NSString *) stringToConvert {      NSS ...

  2. jsp 传值jsp 数据库 乱码解决的攻略 全套

    jsp传值给jsp中文乱码 传值给数据库乱码的解决方法 所有的用到编码的所有统一utf-8 1.装mysql的时候有选择编码的界面的那个地方选utf-8编码 2 建数据库的时候选择 字符集 排序规则所 ...

  3. [UVALive 6661 Equal Sum Sets] (dfs 或 dp)

    题意: 求从不超过 N 的正整数其中选取 K 个不同的数字,组成和为 S 的方法数. 1 <= N <= 20  1 <= K<= 10  1 <= S <= 15 ...

  4. Project Euler:Problem 37 Truncatable primes

    The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...

  5. 2016.3.17__ JavaScript基础_1__第十二天

    Javascript基础 首先说声抱歉. 今日涉及内容难易度不统一,所以很多比較基础的属性直接通过思维导图展示了. 同一时候须要注意,今日思维导图中的内容和笔记中并非一一相应的,请读者自行对比查看. ...

  6. ROW_NUMBER (Transact-SQL)

    SELECT ROW_NUMBER() OVER ( ORDER BY MemberID DESC ) , * FROM dbo.tbm_mem_Member_Beneficiary ORDER BY ...

  7. 【BZOJ 2724】 蒲公英

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2724 [算法] 分块算法在线维护区间众数 分块算法的精髓就在于 : 大段维护,局部朴 ...

  8. Java IO 基础

    早上复习了IO.NIO.AIO相关的概念,将其中一些要点记录一下. 从编程语言层面 BIO | NIO | AIO 以Java的角度,理解,linux c里也有AIO的概念(库),这些概念不知道什么原 ...

  9. SQL语句之Group By

    1. Group By 语句简介: Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若 ...

  10. Tomcat 初探(一) 简介

    简述 大部分入了 Java 开发这个坑的朋友,都需要把自己的网站发布到 Web 服务器上,相信也听过 Tomcat 的鼎鼎大名.Tomcat 是由 Sun 公司软件架构师詹姆斯·邓肯·戴维森开发的 W ...