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

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

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

import sys
while True:
    try:
        s1 = input().strip()
        s2 = input().strip()
        #从短的字符串中获取子串,看是否在长串里
        if len(s1) > len(s2):
            s1, s2 = s2, s1
        s_len = len(s1)
        test_len = s_len
        found = False
        #从最长的算起,如果在长串里就是最长的
        while test_len > 0:
            for i in range(s_len - test_len + 1):
                if s1[i:i + test_len] in s2:
                    print(s1[i:i + test_len])
                    found = True
                    break
            if found:
                break
            test_len -= 1
    except:
#         print(sys.exc_info())
        break















全部评论

相关推荐

投递京东等公司10个岗位
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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