s1 = "werwerwerwrwerwrwrwe"
s2 = "werw"
dp = [[0]*len(s1) for _ in range(len(s2))]
for i in range(len(s1)):# 初始化第一行
if s1[i] == s2[0]:
dp[0][i] = 1
for i in range(len(s2)):# 初始化第一列
if s2[i] == s1[0]:
dp[i][0] = 1
maxlen = 0
for i in range(1, len(s2)):
for j in range(1, len(s1)):
if s2[i] == s1[j]:
dp[i][j] = dp[i-1][j-1] + 1
maxlen = max(maxlen,dp[i][j])
print(maxlen)
求公共子串的最大长度
最新推荐文章于 2024-08-29 19:30:55 发布