描述
给定两个只包含小写字母的字符串,计算两个字符串的最大公共子串的长度。
注:子串的定义指一个字符串删掉其部分前缀和后缀(也可以不删)后形成的字符串。
数据范围:字符串长度:1≤𝑠≤150 1≤s≤150
进阶:时间复杂度:𝑂(𝑛3) O(n3) ,空间复杂度:𝑂(𝑛) O(n)
输入描述:
输入两个只包含小写字母的字符串
输出描述:
输出一个整数,代表最大公共子串的长度
示例1
输入:
asdfas werasdfaswer
输出:
6
string1=input()
string2=input()
if len(string1)<=len(string2):
shortstring=string1
longstring=string2
else:
shortstring=string2
longstring=string1
lengthlst=[]
for i in range(len(shortstring)):
for j in range(len(longstring)):
nowindex=i
nowstartindex=j
length=0
while nowindex<len(shortstring) and nowstartindex<len(longstring) and shortstring[nowindex]==longstring[nowstartindex]:
length+=1
nowindex+=1
nowstartindex+=1
lengthlst.append(length)
print(max(lengthlst))