题解 | #公共子串计算#

公共子串计算

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

动态规划解

  1. 创建dp增广矩阵
  2. 演算推理动态规划递推关系式
while True:
    try:
        s1, s2, ans = input(), input(), 0
        dp = [[0 for i in s1+'0'] for j in s2+'0']
        for j in range(len(s1)):
            for i in range(len(s2)):
                if s1[j] == s2[i]:
                    dp[i+1][j+1] = dp[i][j] + 1
                if dp[i+1][j+1] > ans:
                    ans = dp[i+1][j+1]
        print(ans)
    except EOFError:
        break
全部评论

相关推荐

大野鸡:其实就是量,但是时间有限,1000题只要不是全中等简单,简单中等困难1-2-1,大概能打打比赛了(前20%),10000题就是下一个灵神
点赞 评论 收藏
分享
只因飞飞:今日首绷
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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