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. Java String startsWith()方法

    描述: 这个方法有两个变体并测试如果一个字符串开头的指定索引指定的前缀或在默认情况下从字符串开始位置. 语法 此方法定义的语法如下: public boolean startsWith(String ...

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

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

  3. js图片轮播效果常见的产品无缝轮播

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  4. 错误消息 This computer doesn't have VT-X/AMD-v enabled

    在VirtualBox的Ubuntu虚拟机里试图本地安装Kyma(一个基于Kubernetes的开源框架)时,遇到下面的错误信息: E0827 11:19:38.972489 3093 start.g ...

  5. 【转】Delphi 文件读写

    procedure TForm1.Button1Click(Sender: TObject); variFileHandle: Integer;iFileLength: Integer;iBytesR ...

  6. 普通用户切换到root用户

    普通用户切换到root用户首先按组合键 CTRL+ALT+T 进入终端界面,一般终端界面默认为普通用户权限模式,如何从普通用户进入root用户首先重置root密码输入 sudo passwd root ...

  7. Openjudge-4151-电影节

    这个题是一道贪心的题目,我们要想看的电影数目最多,我们肯定每次都要选最早结束的电影,这样我们才能去看下一部电影. 它本身最早结束,如果同时开始,那肯定是它的放映时间比较短,如果它后开始,先结束,那它的 ...

  8. <Spring Data JPA>一 JPA原生

    1.pom依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  9. NFS和DHCP服务

    1. NFS NFS,Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS: NFS允许一个系统在网络上与他人共享目录和文件 ...

  10. Django 动态建表

    #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Random_lee # -*- coding: utf-8 -*- from django ...