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

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

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

'''
遍历较短的字符串,
从字符串两端开始截取子串,
判断当前子串是不是公共子串,
然后依据子串的长度判断是否是最长公共子串,
每次保留最长的公共子串。
'''

str1 = input()
str2 = input()

def max_list(s1, s2):

    s = ''
    n = len(s2)
    for i in range(n):
        for j in range(n):
            if s1.find(s2[i:n-j]) < 0:
                continue
            else:
                if len(s2[i:n-j]) > len(s):
                    s = s2[i:n-j]
    
    return s

def repeat_list(s1, s2):

    if len(s1) > len(s2):
        s = max_list(s1, s2)
    else:
        s = max_list(s2, s1)
    
    return s

print(repeat_list(str1, str2))

#华为机试#
全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
05-27 14:57
西北大学 golang
强大的社畜在走神:27届真不用急,可以搞点项目、竞赛再沉淀沉淀,我大二的时候还在天天打游戏呢
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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