最长公共连续子串是什么?
- 内容介绍
- 文章标签
- 相关推荐
本文共计256个文字,预计阅读时间需要2分钟。
输入:牛牛有两个字符串(可能包含空格),牛牛想找出其中最长的公共连续子串,希望你能帮助它,并输出其长度。
输入描述:输入为两行字符串,每行长度均小于等于50。
牛牛有两个字符串(可能包含空格),牛牛想找出其中最长的公共连续子串,希望你能帮助他,并输出其长度。输入描述:输入为两行字符串(可能包含空格),长度均小于等于50.
输出描述:输出为一个整数,表示最长公共连续子串的长度。示例1输入abcdeabgde输出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)本文共计256个文字,预计阅读时间需要2分钟。
输入:牛牛有两个字符串(可能包含空格),牛牛想找出其中最长的公共连续子串,希望你能帮助它,并输出其长度。
输入描述:输入为两行字符串,每行长度均小于等于50。
牛牛有两个字符串(可能包含空格),牛牛想找出其中最长的公共连续子串,希望你能帮助他,并输出其长度。输入描述:输入为两行字符串(可能包含空格),长度均小于等于50.
输出描述:输出为一个整数,表示最长公共连续子串的长度。示例1输入abcdeabgde输出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)
