题解 | #最大公共子串#
最大公共子串
https://www.nowcoder.com/practice/e947e4801fc64808b2390c1cf2754906
s1, s2 = input().split(',') # 输入
# 初始化dp前缀二维数组
dp = [[0 for j in range(len(s2))] for i in range(len(s1))]
for i in range(len(s1)):
if s1[i] == s2[0]:
dp[i][0] = 1
for j in range(len(s2)):
if s2[j] == s1[0]:
dp[0][j] = 1
# 记录最大值
temp_max = 0
# DP遍历LCS
def lcs():
global temp_max
for i in range(1,len(s1)):
for j in range(1,len(s2)):
# print(f"dp[{i}][{j}] = {dp[i][j]}")
if s1[i] == s2[j]:
dp[i][j] = dp[i-1][j-1] +1
if dp[i][j] > temp_max:
temp_max = dp[i][j]
lcs()
print(temp_max)