leetCode 72.Edit Distance (编辑距离) 解题思路和方法
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。(详见我同学的博客)
事实上一个替换操作能够相当于一个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];
}
}
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 (编辑距离) 解题思路和方法的更多相关文章
- [LeetCode] 72. Edit Distance 编辑距离
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- [LeetCode] 72. Edit Distance(最短编辑距离)
传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...
- 【LeetCode】72. Edit Distance 编辑距离(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 记忆化搜索 动态规划 日期 题目地址:http ...
- LeetCode - 72. Edit Distance
最小编辑距离,动态规划经典题. Given two words word1 and word2, find the minimum number of steps required to conver ...
- [leetcode]72. Edit Distance 最少编辑步数
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 72. Edit Distance(编辑距离 动态规划)
Given two words word1 and word2, find the minimum number of operations required to convert word1 to ...
- 第十八周 Leetcode 72. Edit Distance(HARD) O(N^2)DP
Leetcode72 看起来比较棘手的一道题(列DP方程还是要大胆猜想..) DP方程该怎么列呢? dp[i][j]表示字符串a[0....i-1]转化为b[0....j-1]的最少距离 转移方程分三 ...
- [leetcode] 72. Edit Distance (hard)
原题 dp 利用二维数组dp[i][j]存储状态: 从字符串A的0~i位子字符串 到 字符串B的0~j位子字符串,最少需要几步.(每一次删增改都算1步) 所以可得边界状态dp[i][0]=i,dp[0 ...
- leetCode 48.Rotate Image (旋转图像) 解题思路和方法
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
随机推荐
- mbed
mbed介绍--ARM最新面向IOT的RTOS与平台 文章为本人原创,转载请注明:http://blog.csdn.net/guo8113/article/details/40479303 mbed是 ...
- Django学习笔记(一)——安装,创建项目,配置
疯狂的暑假学习之 Django学习笔记(一) 教材 书<The Django Book> 视频:csvt Django视频 1.创建项目 django‐admin.py startpro ...
- C#趣味程序---三色球问题
问题:若一个口袋中放有12个球,3红3白和6黑,问从袋中随意取8个球,有多少种不同的颜色搭配? using System; namespace ConsoleApplication1 { class ...
- 2015.04.16,外语,读书笔记-《Word Power Made Easy》 11 “如何辱骂敌人” SESSION 28
TEASER PREVIEW (Teaser 片头,预告片,玩笑 Teaser trailer:预告片) 如何称呼这些人: 完全盲目的服从(obedience [әu'bi:diәns] n. 服从, ...
- angularjs1--动画
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- SQL语句之Group By
1. Group By 语句简介: Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然后针对若 ...
- 转:如何在Ubuntu 14.04中安装最新版Eclipse
想必很多开发人员都知道,Ubuntu 软件源中提供的并不是最新版本的 Eclipse,本教程就教大家如何在 Ubuntu 14.04 中快速安装 Eclipse 官方发布的最新版本. 到目前为止,Ec ...
- 利用IOC—— Castle进行对象映射,以及结合Nhibernate访问数据库
相信很多人对IOC这个概念并不陌生,简而言之其核心就是利用反射来创建对象来实现解耦. 具体这么做解耦的好处是什么,因为鄙人做的项目还不多,所以还没体会到. 但好的项目大概是这样的 就是实现“高内聚,低 ...
- IIS7.0与AP.NET
IIS7在请求的监听和分发机制上进行了革新性的改进,主要体现在对于Windows进行激活服务(Windows Process Activation Service ,WAS)的引入,将原来的W3SVC ...
- xbox 相关
https://live.xbox.com/zh-CN/avatareditor xboxgames://