Python3 计算字符串距离
计算字符串的距离
http://www.nowcoder.com/questionTerminal/3959837097c7413a961a135d7104c314
使用动态规划计算,具体内容参见博客:
https://www.jianshu.com/p/9a53f32cf62b
def editDistance(str1, str2):
'''
计算字符串str1和str2的编辑距离
'''
edit = [[i+j for j in range(len(str2)+1)] for i in range(len(str1)+1)]
for i in range(1,len(str1)+1):
for j in range(1,len(str2)+1):
if str1[i-1] == str2[j-1]:
d = 0
else:
d = 1
edit[i][j] = min(edit[i-1][j]+1,edit[i][j-1]+1,edit[i-1][j-1]+d)
return edit[len(str1)][len(str2)]
while True:
try:
print(editDistance(input(), input()))
except:
break
安克创新 Anker公司福利 814人发布