HJ52 计算字符串的编辑距离
while True:
try:
s1 = input()
s2 = input()
l1 = len(s1)
l2 = len(s2)
dp = [[0]*(l1+1) for _ in range(l2+1)]
for i in range(l1+1):
dp[0][i] = i
for i in range(l2+1):
dp[i][0] = i
for i in range(1,l2+1):
for j in range(1,l1+1):
if s2[i-1] == s1[j-1]:
dp[i][j] = dp[i-1][j-1]
else:
dp[i][j] = min(dp[i-1][j-1], dp[i][j-1], dp[i-1][j])+1
print(dp)
except:
break
HJ65 查找两个字符串a,b中的最长公共子串
while True:
try:
s1 = input()
s2 = input()
if len(s1) < len(s2):
s1,s2 = s2,s1
l1 = len(s1)
l2 = len(s2)
dp = [[0]*(l1+1) for _ in range(l2+1)]
k = m = 0
for i in range(1,l2+1):
for j in range(1,l1+1):
if s1[j-1] == s2[i-1]:
dp[i][j] = dp[i-1][j-1] + 1
if dp[i][j] > m:
m = dp[i][j]
k = i
print(s2[k-m:k])
except:
break