题解 | 查找两个字符串a,b中的最长公共子串
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
s = input()
t = input()
if len(s) >= len(t): # if-else选出短的那个字符串
short_str, long_str = t, s
else:
short_str, long_str = s, t
common = '' # 存储当前的题目要求子串,下方循环中更新,从左到右遍历必定为第一个
for i in range(len(short_str)): # 对短字符串循环遍历,若在长子串中且长度增加则更新
for j in range(i + 1, len(short_str) + 1):
zi_str = short_str[i:j]
if zi_str in long_str and len(zi_str) > len(common):
common = zi_str
print(common)
