题解 | 公共子串计算
公共子串计算
https://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b
import sys
lines = sys.stdin.readlines()
s1 = lines[0].strip()
s2 = lines[1].strip()
if len(s1) > len(s2):
s1,s2 = s2,s1
length = 1
for length in range(1,len(s1)+1):
has_son_str = False # 每次取新length时需要重置为False
for i in range(len(s1)-length+1):
char = s1[i:i + length]
if char in s2: # 存在公共子串,打破当前对s1的循环,进入对length的循环
has_son_str = True
break
if not has_son_str: # 当length取某值时,对s1遍历无子串,则无超过length-1长度的字段
length = length -1
break
print(length)
出错时先找变量名是否有写错
