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. Proteus与Keil连接及其仿真(有例子哦!)

    记录一下Proteus仿真的一些设置和使用,以方便自己以后复习和大家交流!如有错误,希望大家指正. 1.Proteus软件的安装,这里就不作说明了.

  2. SQL Server2012 T-SQL对分页的增强尝试

    简介 SQL Server 2012中在Order By子句之后新增了OFFSET和FETCH子句来限制输出的行数从而达到了分页效果.相比较SQL Server 2005/2008的ROW_Numbe ...

  3. 我来说说java的NIO

      Java NIO的出现旨在提高文件的读写速度,当然IO用NIO重新实过,所以我们不用显示的调用NIO也能享受这种高效的文件读写. Java NIO的高效得益于其两大"助手":C ...

  4. [Windows Server 2012] 安装IIS8.5及FTP

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:安装IIS ...

  5. SQLite_Home

    SQLite教程 SQLite是一个库,实现了一个独立的软件,serverless zero-configuration.事务SQL数据库引擎.SQLite是世界上最广泛的部署SQL数据库引擎.SQL ...

  6. SQLite - SELECT查询

    SQLite - SELECT查询 SQLite SELECT语句用于获取数据从一个SQLite数据库表返回数据结果表的形式.也称为result-sets这些结果表. 语法 SQLite SELECT ...

  7. Unity3D_最简单的开始界面_结束界面

    开始界面1.创建一个新的场景添加button 2.C#脚本LoadingGame.cs using System.Collections;using System.Collections.Generi ...

  8. core下的routelink

    core mvc中 routelink返回和 framework mvc中返回的不一样,core中返回 IHtmlContent, 而 fw 中返回 MvcHtmlString 在写分页方法中用到了r ...

  9. vue 数据没有驱动视图?

    Part.1  问题 数据改变,视图却没有根据数据而改变. 原因在于,数据并不在 vue 监听范围之内,vue 只对事先在 data 中声明的变量丶对象等类型数据进行监听 Part.2  例子 < ...

  10. mybatis 实现批量更新

    更新单条记录 1 UPDATE course SET name = 'course1' WHERE id = 'id1'; 更新多条记录的同一个字段为同一个值 1 UPDATE course SET  ...