牛牛有两个字符串(可以包含空格)。牛牛希望找到最长的公共邻接子串。我希望你能帮助他,并输出它的长度。输入说明:输入长度小于等于50的两行字符串(可以包含空格)。
[En]
Niuniu has two strings (which may contain spaces). Niuniu wants to find the longest common contiguous substring. I hope you can help him and output its length. Input description: enter a two-line string (which may contain spaces) with a length less than or equal to 50.
输出描述: 输出为一个整数,表示最长公共连续子串的长度。 示例1 输入 abcde abgde 输出 2
str1 = input()
str2 = input()
li = [[0 for i in range(len(str2)+1)] for j in range(len(str1)+1)]
res = 0
for i in range(1, len(str1)+1):
for j in range(1,len(str2)+1):
if str1[i-1] == str2[j-1]:
li[i][j] = li[i-1][j-1] + 1
res = max(res, li[i][j])
else:
li[i][j] = 0
print(res)
Original: https://blog.51cto.com/heliotopekxy/5580043
Author: KXYHeliotrope
Title: 最长公共连续子串
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500733/
转载文章受原作者版权保护。转载请注明原作者出处!