描述:
输出两个字符串中同有字符串的最大长度
输入:
两个字符串,用空格隔开
输出:
共有的字符串的最大长度,如果没有,则输出0
def booo():
a, b = input().split(' ')
if len(a) < len(b):
a, b = b, a
start_num = 0
list_out = []
for i in range(1, len(b)+1):
c = b[start_num:i]
if c in a:
list_out.append(len(c))
else:
start_num = i - 1
list_out.sort()
if list_out:
print(list_out[-1])
else:
print(0)
if __name__ == '__main__':
booo()
本文介绍了一种用于寻找两个字符串中共有子串最大长度的算法。通过遍历较短字符串的所有子串,并检查这些子串是否存在于较长字符串中,该算法能够找到最长的共有子串。如果存在多个相同长度的最长子串,将输出其中任意一个的长度。
1152

被折叠的 条评论
为什么被折叠?



