[LeetCode]72. 编辑距离(DP)

题目

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

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

插入一个字符
删除一个字符
替换一个字符
示例?1:

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/edit-distance
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

  • dp

    • 状态
      dp[i][j]表示word1[0,i-1]变为word2[0,j-1]需要的最少编辑距离,特别的dp[0][j]表示从空串变为word2[0,j-1]需要的最少距离。
    • 转移方程
    dp[i][j] = dp[i - 1][j - 1]  ,word[i-1]=word2[j-1]
    dp[i][j] = Math.min(Math.min(dp[i - 1][j - 1] , dp[i - 1][j]), dp[i][j - 1]) + 1,word[i-1]!=word2[j-1]
    • 初始化
          for (int j = 0; j <= word2.length(); ++j) {
              dp[0][j] = j;
          }
          for (int i = 0; i <= word1.length(); ++i) {
              dp[i][0] = i;
          }

代码

class Solution {
    public int minDistance(String word1, String word2) {
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];
        for (int j = 0; j <= word2.length(); ++j) {
            dp[0][j] = j;
        }
        for (int i = 0; i <= word1.length(); ++i) {
            dp[i][0] = i;
        }

        for (int i = 1; i <= word1.length(); ++i) {
            for (int j = 1; j <= word2.length(); ++j) {
                if (word2.charAt(j - 1) == word1.charAt(i - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = Math.min(Math.min(dp[i - 1][j - 1], dp[i - 1][j]), dp[i][j - 1]) + 1;
                }
            }
        }
        return dp[word1.length()][word2.length()];
    }
}

原文地址:https://www.cnblogs.com/coding-gaga/p/12267438.html

时间: 2024-11-07 11:28:14

[LeetCode]72. 编辑距离(DP)的相关文章

[Leetcode 72]编辑距离 Edit Distance

[题目] Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1

leetcode 72. 编辑距离

/***** 定义状态: DP[i][j]其中i表示word1前i个字符,j表示Word2前i个字符 DP[i][j]表示单词1前i个字符匹配单词2前j个字符,最少变换次数: 状态转移: for i:[0,m] for j:[0,n] if(word1[i-1]==word2[j-1]) DP[i][j]=DP[i-1][j-1]; else DP[i][j]=min(DP[i-1][j],DP[i][j-1],DP[i-1][j-1])+1; return DP[m][n]; ******/

编程之美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 character b) Delete a char

【leetcode】编辑距离

dp方程“ 1.初始化:dp[0][i]=i; dp[j][0]=j; 2.dp[i][j]=         dp[i-1][j-1](相等) dp[i-1][j]+1 ,,dp[i][j-1]+1; dp[i-1][j-1] (这个对应是改的况) 注意字符串下标开始位置就OK了 1 public class Solution { 2 public int minDistance(String word1, String word2) { 3 char c1[]=word1.toCharArr

POJ 3356 AGTC 最短编辑距离 DP

http://poj.org/problem?id=3356 题意: 给两个长度不大于1000的串,修改其中一个串使得两串相同,问最少修改次数.修改有三种,插入一个字符,删除一个字符,改变一个字符. 分析: 直接给方程. dp[i][j]表示第一个串前i位和第二串前j位匹配的最小修改次数. dp[0][0] = 0, dp[length(x)][length(y)]为答案. dp[i][j] = min(dp[i-1][j-1] + x[i] != y[j], dp[i-1][j] + 1, d

poj3356 字符串的最小编辑距离 dp

poj3356 字符串的最小编辑距离  dp AGTC Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10895   Accepted: 4188 Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given bel

Codeforces 67C Sequence of Balls 编辑距离 dp

题目链接:点击打开链接 有一个交换操作比较特殊,所以记录每个点距离自己最近的那个字符的位置 然后交换就相当于把第一行要交换的2个字符 之间的字符都删掉 把第二行要交换的2个字符 之间的字符都插入第一行的2个字符之间 然后再进行交换. #include <cstdio> #include <cstring> #include<iostream> using namespace std; #define inf 10000000 #define N 4005 #define

POJ 3356 AGTC (编辑距离 DP)

Description Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below: Deletion: a letter in x is missing in y at a corresponding position. Insertion: a letter in y is missing in

Codeforces 56D Changing a String 编辑距离 dp

题目链接:点击打开链接 编辑距离,,== 一边dp一边记录前驱太累,,还是dp后找路径大法好 #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll int #define N 1010 char s[N], t[N]; int dp[N][N], n, m; // 0为插入 1为删除 2 3为替换 stru