NC35. 编辑距离(二)
描述
示例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]; } };