最长公共连续字串

最长公共连续字串(longest common substr)

比如:“abcdefg”和“bcdfddd”的最长公共连续字串为“bcd”

// 最长公共连续字串
int loggest_common_substr(string s1, string s2){
    int len_s1 = s1.length();
    int len_s2 = s2.length();

    vector<vector<int>> dp(len_s1,vector<int>(len_s2,0));
    int max_len = 0;

    for(int i=0;i<len_s1;i++){
        for(int j=0;j<len_s2;j++){
            if(i==0||j==0)
                dp[i][j] = 0;
            else if(s1[i]==s2[j]){
                dp[i][j] = dp[i-1][j-1]+1;
                max_len = max(max_len, dp[i][j]);
            }else
                dp[i][j] = 0;
        }
    }

    return max_len;
}

int main(int argc, char const *argv[]){

    cout<<loggest_common_substr("accdsas","bccdacc")<<endl;
    
    return 0;
}
posted @ 2020-09-18 21:50  real-zhouyc  阅读(120)  评论(0编辑  收藏  举报