动态规划——Edit Distance

大意:给定两个字符串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

 1 int minDistance(string word1,string word2){
 2     int wlen1 = word1.size();
 3     int wlen2 = word2.size();
 4
 5     int**dp = new int*[wlen1 + 1];
 6     for (int i = 0; i <= wlen1; i++)
 7         dp[i] = new int[wlen2 + 1];
 8
 9     //int dp[maxn][maxn] = { 0 };
10     for (int i = 0; i <= wlen1; i++)
11         dp[i][0] = i;
12     for (int j = 0; j <= wlen2; j++)
13         dp[0][j] = j;
14     int temp = 0;
15     for (int i = 1; i <= wlen1; i++){
16         for (int j = 1; j <= wlen2; j++){
17             if (word1[i - 1] == word2[j - 1])dp[i][j] = dp[i - 1][j-1];
18             else{
19                 temp = dp[i - 1][j - 1]<dp[i - 1][j] ? dp[i - 1][j - 1] : dp[i - 1][j];
20                 temp = temp < dp[i][j - 1] ? temp : dp[i][j - 1];
21                 dp[i][j] = temp + 1;
22             }
23         }
24     }
25
26     /*
27     for (int i = 0; i <= wlen1; i++)
28         delete[]dp[i];
29     delete[]dp;
30     */
31
32     return dp[wlen1][wlen2];
33 }

原文地址:https://www.cnblogs.com/messi2017/p/9892109.html

时间: 2024-10-16 01:33:29

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

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

Edit Distance (or Levenshtein Distance) python solution for leetcode EPI 17.2

https://oj.leetcode.com/problems/edit-distance/ 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

Edit Distance leetcode java

题目: 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) R

判断两个字符串的edit distance为1

from https://leetcode.com/problems/one-edit-distance/ 正如摘要所说,edit distance是通过变换某些位置的字符使得两个字符串相同的操作数:而该问题是一个比较有名的动态规划问题:分别用s和t表示两个字符串,s(i) 表示s中得第i个字符:用f(i, j)表示s(i)和t(j)的edit distance:那么有以下的递推关系: f(i, j) = min ( f(i - 1, j) + 1; //即删掉s中得第i个字符: f(i, j

Min Edit Distance

Min Edit Distance ----两字符串之间的最小距离 PPT原稿参见Stanford:http://www.stanford.edu/class/cs124/lec/med.pdf Tips:由于本人水平有限,对MED的个人理解可能有纰漏之处,请勿尽信. Edit:个人理解指编辑之意,也即对于两个字符串,对其中的一个进行各种编辑操作(插入.删除.替换)使其变为另一个字符串.要解决的问题是求出最小的编辑操作次数是多少. 基因系列比对 定义距离: X,Y是大小分别为n,m的字符串. 定

最小编辑距离(Minimum edit distance)

最小编辑距离是计算欧式距离的一种方法,可以被用于计算文本的相似性以及用于文本纠错,因为这个概念是俄罗斯科学家 Vladimir Levenshtein 在1965年提出来的,所以编辑距离又称为Levenshtein距离. 简单的理解就是将一个字符串转换到另一个字符串所需要的代价(cost),付出的代价越少表示两个字符串越相似,编辑距离越小,从一个字符串转换到另一个字符串简单的归纳可以有以下几种操作,1.删除(delete)2.插入(insert)3.修改(update),其中删除和插入的代价可以

Edit Distance -- LeetCode

原标题链接: http://oj.leetcode.com/problems/edit-distance/ 这道题求一个字符串编辑成为还有一个字符串的最少操作数,操作包含加入,删除或者替换一个字符.这道题难度是比較大的,用常规思路出来的方法一般都是brute force,并且还不一定正确. 这事实上是一道二维动态规划的题目.模型上确实不easy看出来.以下我们来说说递推式. 我们维护的变量res[i][j]表示的是word1的前i个字符和word2的前j个字符编辑的最少操作数是多少.如果我们拥有

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

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 char

【leetcode刷题笔记】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 characterb) Delete a characterc) Replace