题解 | #公共子串计算#双指针的方法

公共子串计算

http://www.nowcoder.com/practice/98dc82c094e043ccb7e0570e5342dd1b

while True:

try:
    str1, str2 = input(), input()
    # 先把str1设为短的字符串
    if len(str1) >= len(str2):
        str1, str2 = str2, str1
    substr = ''
    # 双指针相向查找
    for i in range(len(str1)):
         for j in range(len(str1) - 1, i - 1, -1):
            if str1[i: j + 1] in str2 and j + 1 -i > len(substr):
                substr = str1[i: j + 1]
    print(len(substr))
except:
    break
全部评论

相关推荐

评论
3
1
分享

创作者周榜

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