题解 | #公共子串计算#
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
#搜索法
s1 = input()
s2 = input()
if len(s1) <+ len(s2): #判断最短字符串,公共字符串一定会包含在最短字符串中,减小搜索空间
s_find = s1
s_co = s2
else:
s_find = s2
s_co = s1
num = 0
index = 0 #在长串中开始搜索的索引
for i in range(1, len(s_find)+1): #从长度为1开始搜索公共字符串
for j in range(index, len(s_find)+1 - i): #在长串中搜索长度为i的公共子串
item = s_find[j:j+i]
if item in s_co:
num = i
index = j #若子串存在,直接停止搜索,从当前位置开始i+1长度子串搜索
break #前面的因为更短的子串都不存在,所以不用搜
print(num)
