题解 | #查找两个字符串a,b中的最长公共子串#
查找两个字符串a,b中的最长公共子串
https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506
def child(x): ####求个最长子串
l = []
for i in range(len(x)):
for j in range(i+1,len(x)+1):
l.append(x[i:j])
return l
while True:
try:
a = str(input())
b = str(input())
ll = [] ####小的子串in大的保存
if len(a) > len(b):
s1 = b
s2 = a
else:
s2 = b
s1 = a
for i in child(s1):
if i in s2:
ll.append(i)
print(sorted(ll,key=len,reverse=True)[0]) ###按字符串长度排序,长度相同,默认取第一个
except:
break
叮咚买菜工作强度 221人发布
查看5道真题和解析