ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-08-11 20:41:29  阅读:220  来源: 互联网

标签:LCS int str2 str1 c++ 序列 最长 dp lcs


最长公共子序列

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

思路

动态规划
LCS(m,n)LCS(m ,n)LCS(m,n)表示S1[0...m]S1[0...m]S1[0...m]和S2[0...n]S2[0...n]S2[0...n]的最长公共子序列的长度
S1[m]==S2[n]:LCS(m,n)=1+LCS(m1,n1)S1[m] == S2[n]: LCS(m, n) = 1 + LCS(m - 1, n - 1)S1[m]==S2[n]:LCS(m,n)=1+LCS(m−1,n−1)
S1[m]!=S2[n]:LCS(m,n)=max(LCS(m1,n),LCS(m,n1))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(m−1,n),LCS(m,n−1))

代码

#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的序列,并将其存在vector中
    // 这里形参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

思路

动态规划
str1[i1]==str2[j1]str1[i-1] == str2[j-1]str1[i−1]==str2[j−1]时,dp[i][j]=dp[i1][j1]+1dp[i][j] = dp[i - 1][j - 1] + 1dp[i][j]=dp[i−1][j−1]+1;
只是当str1[i1]=str2[j1]str1[i-1] != str2[j-1]str1[i−1]!=str2[j−1]时,dp[i][j]=0dp[i][j] = 0dp[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;
	}
};

标签:LCS,int,str2,str1,c++,序列,最长,dp,lcs
来源: https://blog.csdn.net/SCS199411/article/details/99222676

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有