Python题解 | #公共子串计算#

公共子串计算

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

import sys
import re

while True:
    try:
        s1 = input().strip()
        s2 = input().strip()
        if len(s1) < len(s2):
            a = s2
            s2 = s1
            s1 = a
        ans = 0
        for i in range(len(s2)):
            for j in range(i + 1, len(s2) + 1):
                if s1.find(s2[i:j]) != -1:
                    ans = max(j - i, ans)
        print(ans)


    except:
        break

全部评论
注意字符串s2的长度为1的情况,确保每个都能被遍历到
点赞 回复 分享
发布于 2024-05-17 08:31 广东

相关推荐

04-19 11:59
门头沟学院 Java
卷不动辣24314:挂,看来不该投这个部门的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务