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

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

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

while True:
    try:
        s1, s2 = input(), input()
    except:
        break
    if len(s1) > len(s2):  # s1短 s2长
        s1, s2 = s2, s1
    M = 0
    result = []
    for i in range(len(s1)):
        for j in range(i, len(s1)):
            if s1[i:j + 1] in s2 and j + 1 - i > M:
                M = j + 1 - i
                result = s1[i:j+1]
    print(result)

全部评论

相关推荐

06-10 23:36
已编辑
首都经济贸易大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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