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题

class Solution {
public:
int minDistance(string word1, string word2) {
int row=word1.size()+;
int col=word2.size()+;
int isEqual=; int dp[row][col];
for(int i=;i<col;++i){
dp[][i]=i;
}
for(int i=;i<row;++i){
dp[i][]=i;
}
for(int i=;i<row;++i)
for(int j=;j<col;++j){
isEqual=(word1[i-]==word2[j-])?:;
dp[i][j]=min(dp[i-][j]+,min(dp[i][j-]+,dp[i-][j-]+isEqual));
}
return dp[row-][col-];
}
};

Edit Distance(动态规划,难)的更多相关文章

  1. 行编辑距离Edit Distance——动态规划

    题目描写叙述: 给定一个源串和目标串.可以对源串进行例如以下操作:  1. 在给定位置上插入一个字符  2. 替换随意字符  3. 删除随意字符 写一个程序.返回最小操作数,使得对源串进行这些操作后等 ...

  2. 动态规划 求解 Minimum Edit Distance

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

  3. leetCode 72.Edit Distance (编辑距离) 解题思路和方法

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

  4. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  5. Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance)

    Leetcode之动态规划(DP)专题-72. 编辑距离(Edit Distance) 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可 ...

  6. Edit Distance——经典的动态规划问题

    题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to conve ...

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

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

  8. [LeetCode] Edit Distance 编辑距离

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

  9. Edit Distance

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

随机推荐

  1. linux系统中文件的几种类型

    Linux系统是以文件的形式来进行管理的.Linux文件类型常见的有:普通文件.目录.字符设备文件.块设备文件.符号链接文件等,如果想了解这方面知识的弟兄,就进来了解了解. Linux系统不同于win ...

  2. RHEL6.4上Samba/NFS服务器简单配置

    近期在RHEL6.4上尝试搭建一个NAS,底层使用XFS文件系统,对外主要提供samba协议和NFS协议共享,这里把主要步骤记录下来. 环境:RHEL6.4,IP:192.168.50.117 1.关 ...

  3. Hessian矩阵与牛顿法

    Hessian矩阵与牛顿法 牛顿法 主要有两方面的应用: 1. 求方程的根: 2. 求解最优化方法: 一. 为什么要用牛顿法求方程的根? 问题很多,牛顿法 是什么?目前还没有讲清楚,没关系,先直观理解 ...

  4. yii 和 zend studio 集成

    yii是基于测试驱动的,而zend studio是一个好用的ide.集成就是必须的. 本文适合喜欢使用ide的开发者,vim用户或者文本编辑器使用者请忽略. 本文使用的是最新的zend studio ...

  5. 查看本机的ip地址

    ifconfig可以查看本机的ip地址:inet addr:10.108.104.185

  6. windows定时执行python脚本

    from:http://blog.csdn.net/Gpwner/article/details/77882131

  7. TWaver可视化编辑器的前世今生(一)电信网管编辑器

    走到今天,TWaver,一个致力于在技术领域(Technology)的弄潮儿(Waver),已经是第十二个年头.当年网吧的小网管都是IDC机房的运维人员了,TWaver也见证了这个时代的成长变迁. 萌 ...

  8. Angular JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)

    方法一和方法二参考:https://www.cnblogs.com/liugang-vip/p/6857595.html 方法一:my-project/node_modules/.bin 下增大内存( ...

  9. Dijkstra算法简单实现(C++)

    图的最短路径问题主要包括三种算法: (1)Dijkstra (没有负权边的单源最短路径) (2)Floyed (多源最短路径) (3)Bellman (含有负权边的单源最短路径) 本文主要讲使用C++ ...

  10. MySQL多版本并发控制(MVCC)

    MVCC是行级锁的一个变种,但是它在很多的情况下避免了加锁操作,因此开销更低.MySQL,包括Oracle.PostgreSQL都实现了MVCC,虽然每个关系数据库实现不一样,但大都是实现了非阻塞的读 ...