动态规划-最长公共子序列&最长公共子串

https://blog.csdn.net/lisonglisonglisong/article/details/41548557

https://www.kancloud.cn/digest/pieces-algorithm/163624

https://zhuanlan.zhihu.com/p/68409952

 

最长公共子串(Longest Common Substring)与最长公共子序列(Longest Common Subsequence)的区别: 子串要求在原字符串中是连续的,而子序列则只需保持相对顺序,并不要求连续。

 

 

 

复制代码
最大公共子序列

#include<iostream>
#include <stdio.h> 
#include <stack> 
#include <string> 
#include <vector> 

using namespace std;


int lcs_lengh(string& str1, string& str2, vector<vector<int> >& dp) {
    if (str1 == "" || str2 == "") {
        return 0;
    }
    for (int i = 0; i <= str1.length(); i++) {
        dp[i][0] = 0;
    }
    for (int j = 0; j <= str2.length(); j++) {
        dp[0][j] = 0;
    }
    for (int i = 1; i <= str1.length(); i++)
        for (int j = 1; j <= str2.length(); j++) {
            if (str1[i-1] == str2[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else {
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            }
        }
    return dp[str1.length()][str2.length()];
}

void print_all_lcs_str(string& str1, string&str2, vector<vector<int> >& dp, int i, int j, string lcs_str) {
    while(i>0 && j>0) {
        if (str1[i-1] == str2[j-1]) {

            lcs_str = str1[i-1] + lcs_str;
            i--;
            j--;
        }
        else if (dp[i-1][j] > dp[i][j-1]) {
            i--;
        }
        else if (dp[i][j-1] > dp[i-1][j]) {
            j--;
        }
        else {
            print_all_lcs_str(str1, str2, dp, i-1, j, lcs_str);
            print_all_lcs_str(str1, str2, dp, i, j-1, lcs_str);
            return;
            
            //cout << "   " << lcs_str << endl;
        }
    }
    cout << "   " << lcs_str << endl;
}


int main()
{
    string str1 = "abcdafafsf";
    string str2 = "afksacdfads";

    vector<vector<int> > dp(str1.length()+1, vector<int>(str2.length()+1));
    int len = lcs_lengh(str1, str2, dp);
    cout << len << endl;

    string lcs_str;
    print_all_lcs_str(str1, str2, dp, str1.length(), str2.length(), lcs_str);


    return 0;
}
复制代码

 

复制代码
最长公共子串
#include<iostream>
#include <stdio.h> 
#include <stack> 
#include <string> 
#include <vector> 

using namespace std;


int common_str_lengh(string& str1, string& str2, vector<vector<int> >& dp) {
    
    int max_common_len = 0;
    if (str1 == "" || str2 == "") {
        return 0;
    }
    for (int i = 0; i <= str1.length(); i++) {
        dp[i][0] = 0;
    }
    for (int j = 0; j <= str2.length(); j++) {
        dp[0][j] = 0;
    }
    for (int i = 1; i <= str1.length(); i++)
        for (int j = 1; j <= str2.length(); j++) {
            if (str1[i-1] == str2[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
                if (dp[i][j] > max_common_len) {
                    max_common_len = dp[i][j];
                }
            }
            else {
                dp[i][j] = 0;
            }
        }
    return max_common_len;
}

void print_all_common_str(vector<vector<int> >& dp, int m, int n, int max_common_len, string& str1) {
    string str_temp;
    for(int i = 1; i <= m; i++)
        for(int j = 1; j <= m; j++)
        {
            if (dp[i][j] == max_common_len) {
                int ik = i, jk = j;
                while(dp[ik][jk] >= 1) {
                    str_temp.push_back(str1[ik-1]); // 注意是ik-1
                    ik--;
                    jk--;
                }
                string str(str_temp.rbegin(), str_temp.rend());
                if (str.length() == max_common_len) {
                    cout << str << endl;
                }
            }
        }
       
}

int main()
{
    string str1 = "abcdafafsf";
    string str2 = "afksacdfads";

    vector<vector<int> > dp(str1.length()+1, vector<int>(str2.length()+1));
    int len = common_str_lengh(str1, str2, dp);
    cout << len << endl;

    string lcs_str;
    print_all_common_str(dp, str1.length(), str2.length(), len, str1);


    return 0;
}
复制代码

 

 

posted on   TMatrix52  阅读(130)  评论(0编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示