最长公共子序列c++实现

最长公共子序列

给定两个字符串S1和S2,求两个字符串的最长公共子序列的长度。
输入样例
ABCD
AEBD
输出样例
3
解释
S1和S2的最长公共子序列为ABD,长度为3

思路

动态规划
L C S ( m , n ) LCS(m ,n) LCS(m,n)表示 S 1 [ 0... m ] S1[0...m] S1[0...m] S 2 [ 0... n ] S2[0...n] S2[0...n]的最长公共子序列的长度
S 1 [ m ] = = S 2 [ n ] : L C S ( m , n ) = 1 + L C S ( m − 1 , n − 1 ) S1[m] == S2[n]: LCS(m, n) = 1 + LCS(m - 1, n - 1) S1[m]==S2[n]:LCS(m,n)=1+LCS(m1,n1)
S 1 [ m ] ! = S 2 [ n ] : L C S ( m , n ) = m a x ( L C S ( m − 1 , n ) , L C S ( m , n − 1 ) ) S1[m] != S2[n]: LCS(m, n) = max(LCS(m - 1, n), LCS(m, n - 1)) S1[m]!=S2[n]:LCS(m,n)=max(LCS(m1,n),LCS(m,n1))

代码
#include <iostream>
#include <vector>
#include <set>
using namespace std;

class Solution{
public:
    int lengthOflongestCommonSequence(string& str1, string& str2){
        int m = str1.length(), n = str2.length();
        if(m == 0 || n == 0)
            return 0;
        dp = vector<vector<int> >(m+1, vector<int>(n+1, 0));
        for(int i = 1; i < m+1; ++i){
            for(int j = 1; j < n+1; ++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[m][n];
    }
    // 找出所有的LCS的序列
    // 这里形参lcs_str不可以为引用,这里需要每次调用lcs_str都重新生成一个对象
    void PrintAllLCS(string& str1, string& str2, 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-1][j] < dp[i][j-1])  //向上走
                    --j;
                //此时向上向右均为LCS的元素
                else{
                    PrintAllLCS(str1, str2, i-1, j, lcs_str);
                    PrintAllLCS(str1, str2, i, j-1, lcs_str);
                    return;
                }
            }
        }
        all_lcs.insert(lcs_str);
    }
    vector<vector<int>> dp;
    set<string> all_lcs;
};

int main()
{
    string s1 = "abcfbc";
    string s2 = "abfcab";
    Solution fir;
    string lcs_str;
    int res = fir.lengthOflongestCommonSequence(s1, s2);
    cout << res << endl;
    fir.PrintAllLCS(s1, s2, s1.length(), s2.length(), lcs_str);
    set<string>::iterator iter = fir.all_lcs.begin();
	while (iter != fir.all_lcs.end()) {
		cout << *iter++ << endl;
	}
    return 0;
}
/*
4
abcb
abfb
abfc
*/
最长公共子串

求两个字符串的最长公共子串,要求子串连续
输入例子:
bab
caba
输出例子:
2

思路

动态规划
s t r 1 [ i − 1 ] = = s t r 2 [ j − 1 ] str1[i-1] == str2[j-1] str1[i1]==str2[j1]时, d p [ i ] [ j ] = d p [ i − 1 ] [ j − 1 ] + 1 dp[i][j] = dp[i - 1][j - 1] + 1 dp[i][j]=dp[i1][j1]+1
只是当 s t r 1 [ i − 1 ] ! = s t r 2 [ j − 1 ] str1[i-1] != str2[j-1] str1[i1]=str2[j1]时, d p [ i ] [ j ] = 0 dp[i][j] = 0 dp[i][j]=0

代码
class Solution {
public:
	int lengthOflongestCommonSubstring(string& str1, string& str2){
    	int m = str1.size(), n = str2.size();
    	int res = 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(str1[i-1] == str2[j-1])
                	dp[i][j] = dp[i-1][j-1] + 1;
            	else
                	dp[i][j] = 0;
            	if(res < dp[i][j])
                	res = dp[i][j];
        	}
    	}
    	return res;
	}
};
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值