首页 > 其他 > 详细

NC127 最长公共子串

时间:2021-08-30 11:54:28      阅读:17      评论:0      收藏:0      [点我收藏+]

 

技术分享图片

参考:

https://www.cnblogs.com/fanguangdexiaoyuer/p/11281179.html

class Solution {
public:
    /**
     * longest common substring
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @return string字符串
     */
    string LCS(string str1, string str2) {
        // write code here
        
        vector<vector<int>> dp (str1.size(), vector<int> (str2.size(),0));
        int a = 0;
        int b = 0;
        int res = 0;
        for(int i = 0; i < str1.size(); i++)
               for(int j = 0; j < str2.size(); j++){
                   if(str1[i] == str2[j]){
                       if(i==0||j==0)
                           dp[i][j] = 1;
                       else{
                           dp[i][j] = dp[i-1][j-1]+1;
                       }
                       if(dp[i][j]>res){
                           res = dp[i][j];
                           a = j-res;
                           b = j;
                       }
                   }
                   else{
                       dp[i][j] = 0;
                   }
               }
        return str2.substr(a+1, res);
    }
};

 

NC127 最长公共子串

原文:https://www.cnblogs.com/ymec/p/15202936.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!