HJ75 公共子串计算
题目链接
思路 动态规划。和HJ65题目的解法一样,而且由于只需输出最长字串的个数,处理更简单些。
代码
import sys
a = input()
b = input()
dp = [[0]*(len(a)+1) for _ in range(len(b)+1)]
max_num = 0
for i in range(1, len(b)+1):
for j in range(1, len(a)+1):
if a[j-1] == b[i-1]:
dp[i][j] = dp[i-1][j-1] + 1
if dp[i][j] > max_num:
max_num = dp[i][j]
else:
dp[i][j] = 0
print(max_num)