再编辑距离(一)的基础上增加了权值
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 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整型
#
class Solution:
def minEditCost(self , str1: str, str2: str, ic: int, dc: int, rc: int) -> int:
# write code here
m, n = len(str1), len(str2)
dp = [[0]*(n+1) for _ in range(m+1)]
for i in range(m+1):
for j in range(n+1):
if i==0 and j==0:#处理原点
dp[i][j] = 0
elif i==0:#处理第0行
dp[i][j] = dp[i][j-1]+ic
elif j==0:#处理第0列
dp[i][j] = dp[i-1][j]+dc
elif str1[i-1]==str2[j-1]:#相等时
dp[i][j] = dp[i-1][j-1]
else:#不相等时,插入、删除、替换
dp[i][j] = min(dp[i][j-1]+ic,dp[i-1][j]+dc,dp[i-1][j-1]+rc)
return dp[-1][-1]



京公网安备 11010502036488号