题解 | 查找两个字符串a,b中的最长公共子串

查找两个字符串a,b中的最长公共子串

https://www.nowcoder.com/practice/181a1a71c7574266ad07f9739f791506

s = input()
t = input()

if len(s) >= len(t):    # if-else选出短的那个字符串
    short_str, long_str = t, s
else:
    short_str, long_str = s, t

common = ''    # 存储当前的题目要求子串,下方循环中更新,从左到右遍历必定为第一个
for i in range(len(short_str)):    # 对短字符串循环遍历,若在长子串中且长度增加则更新
    for j in range(i + 1, len(short_str) + 1):
        zi_str = short_str[i:j]
        if zi_str in long_str and len(zi_str) > len(common):
            common = zi_str
print(common)

全部评论

相关推荐

优秀的大熊猫在okr...:多益:此贼,必有同谋,按律,该当连坐!
你不能接受的企业文化有哪...
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务