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. UESTC--1264--人民币的构造(数学规律)

    人民币的构造 Time Limit: 1000MS   Memory Limit: 65535KB   64bit IO Format: %lld & %llu Submit Status D ...

  2. 树状数组(Binary Indexed Tree(BIT))

    先不说别的,这个博客为我学习树状数组提供了很大帮助,奉上传送门 http://blog.csdn.net/int64ago/article/details/7429868 然后就说几个常用的操作 in ...

  3. nginx的安装步骤

    nginx学习资料;https://zhuanlan.zhihu.com/p/34943332 1.下载nginx的安装包:https://nginx.org/en/download.html 2. ...

  4. [C#] 隐式类型var —— 示例解析

    从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,它的具体类型由编译器根据上下文推断而出. 下面就让我来总结下隐式类型的一些特点: 1.va ...

  5. 40.DOM读取XML

    main.cpp #include <QtGui> #include <iostream> #include "domparser.h" int main( ...

  6. WPF动态控件生成查找不到问题

    2012 08 10 遇到此类问题,已经找到解决方案 记录以备后用   动态往界面添加控件 在页面未显示的情况时,虽然对控件增加了id name等属性但是使用 TextBox txtOtherNati ...

  7. 关于flex布局中的兼容性问题

    这几天在做项目中用到了flex布局,但是在测试的过程中发现他的兼容性实在是太差了,仅仅用到水平和垂直居中的样式,没想到兼容性代码就写了好几行. display:flex; display:-webki ...

  8. DOM基础知识(Node对象、Element对象)

    5.Node对象 u  遍历节点 u 父节点 .parentNode - 获取父节点—> 元素节点或文档节点 .parentElement - 获取父元素节点—> 元素节点 u    子节 ...

  9. Android Finalizing a Cursor that has not been deactivated or closed

    问题描述: 使用Sqlite数据库时,有时候会报下面的异常: Finalizing a Cursor that has not been deactivated or closed 一个光标没有被停用 ...

  10. 防范CSRF(三)

    除了可以更改微软默认的cookie的名字外,还可以更改默认的加密方法.继承IAntiForgeryAdditionalDataProvider接口,实现里面的方法即可. 采用的加密方法有加盐和使用时间 ...