题解 | #计算字符串的编辑距离#动态规划
计算字符串的编辑距离
https://www.nowcoder.com/practice/3959837097c7413a961a135d7104c314
a, b = input(), input()
dp = [[0 for _ in range(len(a) + 1)] for _ in range(len(b) + 1)]
for i in range(len(b) + 1):
for j in range(len(a) + 1):
if i == 0:#dp数组第一行初始化赋值
dp[i][j] = j
continue
elif j == 0:#dp数组第一列初始化赋值
dp[i][j] = i
continue
if a[j-1] == b[i-1]:
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
print(dp[len(b)][len(a)])
SHEIN希音公司福利 248人发布