练习1:求2个字符串的最长公共子串;
解法1:矩阵思路
def findint(str1,str2):
length1 = len(str1)
length2 = len(str2)
matrix = [[0]*length1 for i in range(length2)]
xmax = 0
xindex = 0
for i, x in enumerate(str2):
for j, y in enumerate(str1):
if x != y:
pass
else:
if i == 0 or j == 0:
matrix[i][j] = 1
else:
matrix[i][j] = matrix[i-1][j-1] + 1
if matrix[i][j] > xmax:
xmax = matrix[i][j]
xindex = j
strat = xindex + 1 - xmax
end = xindex + 1
print(matrix,xmax,xindex,strat,end)
return str1[start:end]
s1 = 'abcdefg'
s2 = 'defabcd'
网友评论