[题记-动态规划] 编辑距离 - leetcode

题目: 编辑距离

给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。

你可以对一个单词进行如下三种操作:

插入一个字符
删除一个字符
替换一个字符

示例 1:

输入:word1 = "horse", word2 = "ros"
输出:3
解释:
horse -> rorse (将 ‘h‘ 替换为 ‘r‘)
rorse -> rose (删除 ‘r‘)
rose -> ros (删除 ‘e‘)
示例 2:

输入:word1 = "intention", word2 = "execution"
输出:5
解释:
intention -> inention (删除 ‘t‘)
inention -> enention (将 ‘i‘ 替换为 ‘e‘)
enention -> exention (将 ‘n‘ 替换为 ‘x‘)
exention -> exection (将 ‘n‘ 替换为 ‘c‘)
exection -> execution (插入 ‘u‘)



思路:

定义一个数组 pd[i][j] 表示将 word1 前 i 个 字母替换为 word2 前 j 个字母所用的最少修改次数

若两个单词的字母相等: 则  dp[ i ][ j ] = dp[ i - 1 ][ j - 1 ];

否则在三个操作之间去一个最小的:dp[ i ][ j ] = min( min( dp[ i - 1 ][ j ], dp[ i ][ j - 1 ]  ), dp[ i - 1 ][ j - 1 ] );

class Solution {
public:
    int minDistance(string word1, string word2) {
        int len1 = word1.size();
        int len2 = word2.size();

        int dp[len1+1][len2+1];
        memset(dp,0,sizeof(dp));

        for( int i = 0; i <= len1; i++ ) dp[i][0] = i;
        for( int j = 0; j <= len2; j++ ) dp[0][j] = j;

        for( int i = 1; i <= len1; i++ ) {
            for( int j = 1; j <= len2; j++ ) {
                if( word1[ i - 1 ] == word2[ j - 1 ] ) {
                    dp[i][j] = dp[i-1][j-1];
                }
                else {
                     dp[i][j] = min( dp[i-1][j-1], min( dp[i-1][j], dp[i][j-1] ) ) + 1;
                }
            }
        }

        return dp[len1][len2];
    }
};

2020-04-06-15:00:52

原文地址:https://www.cnblogs.com/Sxccz/p/12642155.html

时间: 2024-08-30 13:37:31

[题记-动态规划] 编辑距离 - leetcode的相关文章

动态规划-编辑距离

设A和B是2个字符串.要用最少的字符操作将字符串A转换为字符串B.这里所说的字符操作包括 (1)删除一个字符: (2)插入一个字符: (3)将一个字符改为另一个字符. 将字符串A变换为字符串B所用的最少字符操作数称为字符串A到 B的编辑距离,记为d(A,B). 对于给定的字符串A和字符串B,计算其编辑距离 d(A,B). 输入格式: 第一行是字符串A,文件的第二行是字符串B. 提示:字符串长度不超过2000个字符. 输出格式: 输出编辑距离d(A,B) 输入样例: 在这里给出一组输入.例如: f

【动态规划】leetcode - Maximal Square

称号: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 分析: 利用动态规划求解.建立一个类node.nod

动态规划系列 Leetcode 70. Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1: Input: 2 Output: 2 Explanation:

动态规划--编辑距离算法

设A和B是2个字符串.要用最少的字符操作将字符串A转换为字符串B.这里所说的字符操作包括 (1)删除一个字符: (2)插入一个字符: (3)将一个字符改为另一个字符. 将字符串A变换为字符串B所用的最少字符操作数称为字符串A到 B的编辑距离,记为d(A,B). 对于给定的字符串A和字符串B,计算其编辑距离 d(A,B). 对于每一个对应字符有三种解决方法案: C++代码: #include <iostream> #include <string.h> using namespace

简单线性动态规划 —— 编辑距离

可用滚动数组压缩空间,略 if s[i] = t[j] then dp[i, j] := dp[i-1, j-1]          // no operation required else dp[i, j] := min( dp[i-1, j] + 1,     // a deletion dp[i, j-1] + 1,     // an insertion dp[i-1, j-1] + 1  // a substitution ) 加权的情况: dp[i][j] := min( dp[i

动态规划系列 Leetcode 198. House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之余全部耗在这上面了,只怪自己基础差.本文主要介绍使用Manacher线性算法来求解字符串的最长回文子字符串. 题目 Given a string S, find the longest palindromic substring in S. You may assume that the maxim

2017校招常考算法题归纳&amp;典型题目汇总

2017校招即将来临,我们为大家整理了2017校招的常考算法类型,以及对应的典型题目. 另附参考答案地址:http://www.jiuzhang.com/solution/ 数学 尾部的零 斐波纳契数列 x的平方根 x的平方根 2 大整数乘法 骰子求和 最多有多少个点在一条直线上 超级丑数 比特位操作 将整数A转换为B 更新二进制位 二进制表示 O(1)时间检测2的幂次 二进制中有多少个1 动态规划 编辑距离 正则表达式匹配 交叉字符串 乘积最大子序列 二叉树中的最大路径和 不同的路径 通配符匹

校招的常考算法类型以及对应的典型题目

数学 尾部的零斐波纳契数列x的平方根x的平方根2大整数乘法骰子求和最多有多少个点在一条直线上超级丑数 比特位操作 将整数A转换为B更新二进制位二进制表示O(1)时间检测2的幂次二进制中有多少个1 动态规划 编辑距离正则表达式匹配交叉字符串乘积最大子序列二叉树中的最大路径和不同的路径通配符匹配 堆 滑动窗口的中位数数据流中位数最高频的K个单词接雨水堆化排序矩阵中的从小到大第k个数 二叉树 二叉树中序遍历二叉树的序列化和反序列化子树最近公共祖先二叉树的层次遍历将二叉树拆成链表在二叉查找树中插入节点