题解 | #公共子串计算#

公共子串计算

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

#include <iostream>
#include <vector>
using namespace std;
// 套公式做的 
int main() {
    string str1, str2;
    while (cin >> str1 >> str2) { // 注意 while 处理多个 case
        int len1 = str1.length();
        int len2 = str2.length();
        int max = 0;
        vector<vector<int>> matrix(len1+1, vector<int>(len2+2, 0));
        for(int i = 1; i < len1+1; i++){
                for(int j = 1; j < len2+1; j++){
                    if(str1[i-1] == str2[j-1]){
                        matrix[i][j] = matrix[i-1][j-1] + 1;
                    }else{
                        matrix[i][j] = 0;
                    }
                    
                    if(matrix[i][j] > max){
                        max = matrix[i][j];
                    }
            }
        }

        cout << max << endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

待现的未见之事:起码第一句要把自己的优势说出来吧。比如什么xx本27届学生,随时到岗....
点赞 评论 收藏
分享
被加薪的哈里很优秀:应该继续招人,不会给你留岗位的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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