列表

详情


NC35. 编辑距离(二)

描述

给定两个字符串str1和str2,再给定三个整数ic,dc和rc,分别代表插入、删除和替换一个字符的代价,请输出将str1编辑成str2的最小代价。

数据范围:
要求:空间复杂度 ,时间复杂度

示例1

输入:

"abc","adc",5,3,2

输出:

2

示例2

输入:

"abc","adc",5,3,100

输出:

8

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 4ms, 内存消耗: 376KB, 提交时间: 2021-05-11

class Solution {
public:
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    int minEditCost(string str1, string str2, int ic, int dc, int rc) {
        // write code here
                int m = str1.size();
        int n = str2.size();
        vector<int> dp(n+1,0);
        for(int i = 1; i <= n; i++) {
            dp[i] = i*ic;
        }
        for(int i = 1; i <= m; i++) {
            char c1 = str1[i-1];
            int prev = dp[0];
            dp[0] = i * dc;
            for(int j = 1; j <= n; j++) {
                char c2  = str2[j-1];
                int temp = dp[j];
                if(c1 == c2)
                    dp[j] = prev;
                else {
                    int insert = dp[j-1] + ic;
                    int del = temp + dc;
                    int replace = prev + rc;
                    dp[j] = min(insert,min(del,replace));
                }
                prev = temp;
            }
        }
        return dp[n];
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 376KB, 提交时间: 2021-02-21

class Solution {
public:
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    int minEditCost(string str1, string str2, int ic, int dc, int rc) {
        // write code here
        int m = str1.size();
        int n = str2.size();
        vector<int> dp(n+1,0);
        for(int i = 1; i <= n; i++) {
            dp[i] = i*ic;
        }
        for(int i = 1; i <= m; i++) {
            char c1 = str1[i-1];
            int prev = dp[0];
            dp[0] = i * dc;
            for(int j = 1; j <= n; j++) {
                char c2  = str2[j-1];
                int temp = dp[j];
                if(c1 == c2)
                    dp[j] = prev;
                else {
                    int insert = dp[j-1] + ic;
                    int del = temp + dc;
                    int replace = prev + rc;
                    dp[j] = min(insert,min(del,replace));
                }
                prev = temp;
            }
        }
        return dp[n];
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 388KB, 提交时间: 2021-03-26

class Solution {
public:
    int minEditCost(string &str1, string &str2, int &ic, int &dc, int &rc) {
        int num1 = str1.size(), num2 = str2.size();
        int dp[num2 + 1];
        for(int j = 0; j <= num2; j++) dp[j] = j * ic;
        for(int i = 1; i <= num1; i++ ) {
            int pre = dp[0];
            dp[0] = i * dc;
            for(int j = 1; j <= num2; j++) {
                int insert = dp[j - 1] + ic;
                int del = dp[j] + dc;
                int modify = pre + (str1[i - 1] == str2[j - 1] ? 0 : rc);
                pre = dp[j];
                dp[j] = min(insert, min(del, modify));
            }
        }
        return dp[num2];
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 396KB, 提交时间: 2021-04-07

class Solution {
public:
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    int minEditCost(string str1, string str2, int ic, int dc, int rc) {
        // write code here
        int num1 = str1.size(),num2 = str2.size();
        int dp[num2+1];
        for(int j=0;j<=num2;j++)dp[j]=j*ic;
        for(int i=1;i<=num1;i++){
            int pre = dp[0];
            dp[0]=i *dc;
            for(int j=1;j<=num2;j++){
                int insert = dp[j-1]+ic;
                int del = dp[j]+dc;
                int modify = pre + (str1[i-1]==str2[j-1]?0:rc);
                pre  = dp[j];
             dp[j] = min(insert,min(del,modify));
                
            }
        }
        return dp[num2];
    }
};

C++ 解法, 执行用时: 4ms, 内存消耗: 400KB, 提交时间: 2021-04-09

class Solution {
public:
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    int minEditCost(string str1, string str2, int ic, int dc, int rc) {
        // write code here
        int num1=str1.size(),num2=str2.size();
        int dp[num2+1];
        for(int j=0;j<=num2;j++) dp[j]=j*ic;
        for(int i=1;i<=num1;i++){
            int pre=dp[0];
            dp[0]=i*dc;
            for(int j=1;j<=num2;j++){
                int insert=dp[j-1]+ic;
                int del=dp[j]+dc;
                int modify=pre + (str1[i-1]==str2[j-1]?0:rc);
                pre  = dp[j];
                 dp[j] = min(insert,min(del,modify));
            }
        }
        return dp[num2];
    }
};

上一题