def maxcomstr(s, t):
index = 0
length = 0
i = 0
while i < len(s):
j = 0
while j < len(t):
if s[i] == t[j]:
k = 1
length1 = 1
con = 1
while con:
if i + k < len(s) and j + k < len(t) and s[i + k] == t[j + k]:
length1 = length1 + 1
k = k + 1
else:
con = 0
if length1 > length:
index = i
length = length1
j = j + k
else:
j = j + 1
i = i + 1
return index, length
if __name__ == '__main__':
s = 'acaaciauw'
t = 'bcaacicaw'
index, length = maxcomstr(s, t)
result = s[index:index + length]
print(result)
运行结果:
caaci