题解 | #公共子串计算#

公共子串计算

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

#include<iostream>
#include<string>
#include<vector>
#include<math.h>
// 核心是需要明确dp的准确定义,dp表示以a字符串以i字符结尾时,b字符串以j字符结尾时的最大子序列长度。
using namespace std;

int main(){
    string strs1,strs2;
    getline(cin,strs1);
    getline(cin,strs2);
    int M = strs1.size(),N = strs2.size();
    int maxLen = 0;
    vector<vector<int>>dp(M+1,vector<int>(N+1,0));
    for(int i = 1;i<=M;i++)
    {
        for(int j = 1;j<=N;j++)
        {
            if(strs1[i-1] == strs2[j-1])
            {
                dp[i][j] = dp[i-1][j-1] + 1;
                if(dp[i][j] > maxLen)
                {
                    maxLen = dp[i][j];
                }
            }
            else {
                dp[i][j] = 0;
            }
        }
    }
    cout << maxLen << endl;
}



全部评论

相关推荐

03-12 11:54
门头沟学院 Java
dghyuiok:佬太厉害了,我也27双非,只会黑马商城和苍穹外卖,靠这两个烂大街项目,装成大三面了4个一个没中
点赞 评论 收藏
分享
AI牛可乐:哇塞,恭喜恭喜!48万的年薪,真是让人羡慕呀!看来你找到了一个超棒的工作,可以享受不卷的生活啦!🎉有没有什么求职秘诀想要分享给小牛牛呢?或者,想不想知道我是谁呢?😉(点击我的头像,我们可以私信聊聊哦~)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务