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. BuilderParttern(建造者模式)

    /** * 建造者模式 * 主要用于构造复杂的对象 * 在优朋播放器就是采用建造者构建的,可以说比较有心得吧 * @author TMAC-J * */ public class BuilderPat ...

  2. B树、B+树的实现

    B树的定义 假设B树的度为t(t>=2),则B树满足如下要求:(参考算法导论) (1)  每个非根节点至少包含t-1个关键字,t个指向子节点的指针:至多包含2t-1个关键字,2t个指向子女的指针 ...

  3. 一款简洁大气的jquery日期日历插件

    本jquery插件名为manhuaDate,暂时只支持jquery 1.9.0以下版本,比如jquery-1.8.3.min.js 查看效果网址:http://keleyi.com/a/bjad/em ...

  4. mysql存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

  5. centos虚拟机复制移动后网络配置无效

    移植Centos虚拟机后无法联网解决1.迁移以后,会存在其中一个网卡无法启动(eth0 or eth1) [root@ ~]# ifup eth0 WARNING: Deprecated config ...

  6. Android原生json和fastjson的简单使用

    android原生操作json数据 主要是两个类 JSONObject 操作对象     JONSArray操作json数组 对象转json //创建学生对象 Student student=new ...

  7. 通用的Dialog自定义dialog

    图样:

  8. KVC&&&KVO

    KVC 什么是KVC --->What KVC指的就是NSKeyValueCoding非正式协议. KVC是一种间接地访问对象的属性的机制. 这种间接表现在通过字符串来标识属性,而不是通过调用存 ...

  9. Macbook SSD硬盘空间不够用了?来个Xcode大瘦身吧!

    原文转自:http://www.jianshu.com/p/03fed9a5fc63    日期:2016-04-22 最近突然发现我的128G SSD硬盘只剩下可怜的8G多,剩下这么少的一点空间连X ...

  10. C#.NET万能数据库访问封装类(ACCESS、SQLServer、Oracle)

    using System; using System.Collections; using System.Collections.Specialized; using System.Data; usi ...