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

分析:

处理这道题也是用动态规划。

动态数组dp[word1.length+1][word2.length+1]

dp[i][j]表示从word1前i个字符转换到word2前j个字符最少的步骤数。

假设word1现在遍历到字符x,word2遍历到字符y(word1当前遍历到的长度为i,word2为j)。

以下两种可能性:

1. x==y,那么不用做任何编辑操作,所以dp[i][j] = dp[i-1][j-1]

2. x != y

(1) 在word1插入y, 那么dp[i][j] = dp[i][j-1] + 1

(2) 在word1删除x, 那么dp[i][j] = dp[i-1][j] + 1

(3) 把word1中的x用y来替换,那么dp[i][j] = dp[i-1][j-1] + 1

最少的步骤就是取这三个中的最小值。

最后返回 dp[word1.length+1][word2.length+1] 即可。

class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
vector<vector<int>> dp(m+);
for(size_t i = ; i<m+; i++)
for(size_t j = ; j<n+; j++)
dp[i].push_back(-);
for(size_t i=; i<m+;i++)
dp[i][] =i;
for(size_t j=; j<n+; j++)
dp[][j] =j;
for(int i=; i<m+; i++)
for(int j =; j<n+; j++)
{
if(word1[i-] == word2[j-]){
dp[i][j] = dp[i-][j-];
}
else{
int insert = dp[i-][j]+;
int replace = dp[i-][j-]+;
int del = dp[i][j-]+;
dp[i][j] = min(del,min(insert, replace));
}
}
return dp[m][n];
}
};

Edit Distance的更多相关文章

  1. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  2. [LeetCode] Edit Distance 编辑距离

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

  3. 编辑距离——Edit Distance

    编辑距离 在计算机科学中,编辑距离是一种量化两个字符串差异程度的方法,也就是计算从一个字符串转换成另外一个字符串所需要的最少操作步骤.不同的编辑距离中定义了不同操作的集合.比较常用的莱温斯坦距离(Le ...

  4. LintCode Edit Distance

    LintCode Edit Distance Given two words word1 and word2, find the minimum number of steps required to ...

  5. stanford NLP学习笔记3:最小编辑距离(Minimum Edit Distance)

    I. 最小编辑距离的定义 最小编辑距离旨在定义两个字符串之间的相似度(word similarity).定义相似度可以用于拼写纠错,计算生物学上的序列比对,机器翻译,信息提取,语音识别等. 编辑距离就 ...

  6. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  7. 动态规划 求解 Minimum Edit Distance

    http://blog.csdn.net/abcjennifer/article/details/7735272 自然语言处理(NLP)中,有一个基本问题就是求两个字符串的minimal Edit D ...

  8. One Edit Distance

    Given two strings S and T, determine if they are both one edit distance apart. 分析:https://segmentfau ...

  9. 【leetcode】Edit Distance

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

随机推荐

  1. java.lang.IllegalArgumentException: Illegal character in query at index 261

    在BaseFragment中使用了LoadingPage,而LoadingPage的联网加载使用的是AsyncHttpClient.一直报java.lang.IllegalArgumentExcept ...

  2. CentOS 6.4 服务器版安装教程(超级详细图解)

    附:CentOS 6.4下载地址 32位:http://mirror.centos.org/centos/6.4/isos/i386/CentOS-6.4-i386-bin-DVD1to2.torre ...

  3. .net开发过程中Bin目录下面几种文件格式的解释

    在.NET开发中,我们经常会在bin目录下面看到这些类型的文件: .pdb..xsd..vshost.exe..exe..exe.config..vshost.exe.config 项目发布的时候,往 ...

  4. java web学习总结(四) -------------------HTTP协议

    一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...

  5. Struts2(1) —— 概述

    1.Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发,框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2.Struts2框架的流程原理 ...

  6. iOS 点击TextField不弹出软键盘的解决方案

    开发中遇到: 在模拟器里面,textfield可以通过电脑键盘输入,可是怎么也不会自动弹出模拟器软键盘 解决方案: 切换一下键盘,command+shift+k,Xcode6.3 中只能是一种输入源

  7. IOS开发基础知识--碎片11

    1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...

  8. iOS 陀螺仪,加速度计

    atan2(x, y)反正切函数  x是对边  y临边 data.acceleration  加速度值,当手机水平放置时值为(0,0,-1),当手机竖直放置时值为(0,-1,0),判断手机拿起状态可以 ...

  9. Mac使用极简教程

    最近领导让我写一篇关于Mac的使用教程,因为使用人群未知,所以尽量写的通俗易懂,可谓是关于Mac电脑使用的精简教程吧,在此发表出来以供参考. Mac因为安全性而闻名,我们拥有了一部Mac,那么我们来了 ...

  10. 记CentOS-7-x86_64-DVD-1503与Windows7单硬盘双系统的安装

    我最初的设想是:Win引导CentOS,最后却变成了CentOS引导Win了.算是‘弄拙成巧’了吧. 因为我打算用U盘刻录镜像直接从U盘启动,所以不需要网上一些教程里面的繁琐的win下引导CentOS ...