大意:给定两个字符串word1和word2,为了使word1变为word2,可以进行增加、删除、替换字符三种操作,请输出操作的最少次数
 

Example 1:

Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:

Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')
 
状态:dp[i][j]把word1[0..i-1]转换到word2[0..j-1]的最少操作次数
状态转移方程:
  (1)如果word1[i-1] == word2[j-1],则令dp[i][j] = dp[i-1][j-1]
  (2)如果word1[i-1] != word2[j-1],由于没有一个特别有规律的方法来断定执行何种操作,在增加、删除、替换三种操作中选一种操作次数少的赋值给dp[i][j];
    增加操作:dp[i][j] = dp[i][j-1] + 1
    删除操作:dp[i][j] = dp[i-1][j] + 1
       替换操作:dp[i][j] = dp[i-1][j-1] + 1
 
 int minDistance(string word1,string word2){
int wlen1 = word1.size();
int wlen2 = word2.size(); int**dp = new int*[wlen1 + ];
for (int i = ; i <= wlen1; i++)
dp[i] = new int[wlen2 + ]; //int dp[maxn][maxn] = { 0 };
for (int i = ; i <= wlen1; i++)
dp[i][] = i;
for (int j = ; j <= wlen2; j++)
dp[][j] = j;
int temp = ;
for (int i = ; i <= wlen1; i++){
for (int j = ; j <= wlen2; j++){
if (word1[i - ] == word2[j - ])dp[i][j] = dp[i - ][j-];
else{
temp = dp[i - ][j - ]<dp[i - ][j] ? dp[i - ][j - ] : dp[i - ][j];
temp = temp < dp[i][j - ] ? temp : dp[i][j - ];
dp[i][j] = temp + ;
}
}
} /*
for (int i = 0; i <= wlen1; i++)
delete[]dp[i];
delete[]dp;
*/ return dp[wlen1][wlen2];
}
 
 

动态规划——Edit Distance的更多相关文章

  1. 动态规划 求解 Minimum Edit Distance

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

  2. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

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

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

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

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

  5. [LeetCode] Edit Distance 编辑距离

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

  6. Edit Distance

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

  7. 编辑距离——Edit Distance

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

  8. 【leetcode】Edit Distance

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

  9. 56. Edit Distance && Simplify Path

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

随机推荐

  1. 2733: [HNOI2012]永无乡 线段树合并

    题目: https://www.lydsy.com/JudgeOnline/problem.php?id=2733 题解: 建n棵动态开点的权值线段树,然后边用并查集维护连通性,边合并线段树维护第k重 ...

  2. cdq分治(hdu 5618 Jam's problem again[陌上花开]、CQOI 2011 动态逆序对、hdu 4742 Pinball Game、hdu 4456 Crowd、[HEOI2016/TJOI2016]序列、[NOI2007]货币兑换 )

    hdu 5618 Jam's problem again #include <bits/stdc++.h> #define MAXN 100010 using namespace std; ...

  3. Day038--Python--Gevent , IO多路复用

    1. 协程: gevent  (遇到IO自动切换) import gevent import time from gevent import monkey; monkey.patch_all() # ...

  4. LFYZ-OJ ID: 1024 火车站

    火车过站 问题描述 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数保持为a人.从 ...

  5. [家里蹲大学数学杂志]第057期图像复原中的改进 TV 模型

    $\bf 摘要$: 本文给出了王大凯等编的<图像处理中的偏微分方程方法>第 6.2 节的详细论述. $\bf 关键词$: 图像复原; TV 模型; matlab 编程 1. 前言 图像在形 ...

  6. mui-选项卡+scroll滚动

    详细操作见代码: <!doctype html> <html> <head> <meta charset="UTF-8"> < ...

  7. PYthon3:简单几步实现冒泡排序

    1.概念理解: 冒泡排序:可以简单的理解为是列表中相近的元素,两两比较,小的在前面.最多需要len()-1次排序. 2.例子:a=[11,7,4,56,35,0] 3.代码实现: 4.输出结果: 第1 ...

  8. day 16 - 2 内置函数(二)练习

    内置函数(二)练习 1.用 map 来处理字符串列表,把列表中所有人都变成 sb,比方 alex_sbname=['alex','wupeiqi','yuanhao','nezha'] name=[' ...

  9. MapReduce输出文件名更改

    默认情况下生成的文件名是part-r-00000格式,想要自定义生成输出文件名可以使用org.apache.hadoop.mapreduce.lib.output.MultipleOutputs类用来 ...

  10. 第五节,python基本语法

    1.类(Class) Python中的类的概念与其它语言类似,比较特殊的是protected和private在python中没有明确的限制,通常的惯例是用单下划线_表示protected,用双下划线开 ...