leetcode 1143. Longest Common Subsequence

此题为求最大公共子序列,需要注意的是公共子序列和公共子串概念不同
ad 是 abcd 的公共子序列,而不是公共子串
ab 是 abcd的公共子序列,且是公共子串

如果求最大公共子串,见
https://blog.csdn.net/qq_34662299/article/details/100049330
主要运用动态规划的思想求解,需要注意的两点就是dp转移方程的确定及初始值

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int len1=text1.size();
        int len2=text2.size();
        vector<vector<int >> dp(len1,vector<int>(len2,0));
        
        if(len1==0||len2==0)
            return 0;
        
        
        //建立初始条件
        for(int i=0;i<len1;i++)
        {
            if(text1[i]==text2[0])
            {
                while(i<len1)
                    dp[i++][0]=1;
            }
                
        }
        
        for(int j=0;j<len2;j++)
        {
            if(text1[0]==text2[j])
            {
                while(j<len2)
                    dp[0][j++]=1;
            }
                
        }
           
        //dp填表    
        for(int i=1;i<len1;i++)
            for(int j=1;j<len2;j++)
            {
                if(text1[i]==text2[j])
                    dp[i][j]=dp[i-1][j-1]+1;                   
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        
        return dp[len1-1][len2-1];
    }
};

看了别人代码之后,发现可以不用自己设定初始值,直接扩充一维,代码较精简。

class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        int len1=text1.size();
        int len2=text2.size();
        vector<vector<int >> dp(len1+1,vector<int>(len2+1,0));
        
        if(len1==0||len2==0)
            return 0;    
           
        //dp填表    
        for(int i=0;i<len1+1;i++)
            for(int j=0;j<len2+1;j++)
            {
                if(i==0||j==0)
                    dp[i][j]=0;
                else if(text1[i-1]==text2[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[len1][len2];
    }
};

扩展一下,如果需要求出来其公共子序列具体是啥,则需要从dp表中的结果回溯查找。

/**
 *Copyright @ 2019 Zhang Peng. All Right Reserved.
 *Filename:
 *Author: Zhang Peng
 *Date:
 *Version:
 *Description:
**/

#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

class Solution {
public:
	int longestCommonSubsequence(string text1, string text2) {
		int len1 = text1.size();
		int len2 = text2.size();
		vector<vector<int >> dp(len1 + 1, vector<int>(len2 + 1, 0));

		if (len1 == 0 || len2 == 0)
			return 0;

		//dp填表    
		for (int i = 0; i<len1 + 1; i++)
		for (int j = 0; j<len2 + 1; j++)
		{
			if (i == 0 || j == 0)
				dp[i][j] = 0;
			else if (text1[i - 1] == text2[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[len1][len2];
	}

	vector<vector<int >> longestCommonSubsequenceDp(string text1, string text2) {
		int len1 = text1.size();
		int len2 = text2.size();
		vector<vector<int >> dp(len1 + 1, vector<int>(len2 + 1, 0));

		if (len1 == 0 || len2 == 0)
			return dp;

		//dp填表    
		for (int i = 0; i<len1 + 1; i++)
		for (int j = 0; j<len2 + 1; j++)
		{
			if (i == 0 || j == 0)
				dp[i][j] = 0;
			else if (text1[i - 1] == text2[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;
	}

	void findpath(vector<vector<int>> & dp, string & a, string & b,int i,int j,string & result)
	{
		if (i <=0||j<=0)
			return;
		

		if (dp[i][j]==dp[i-1][j])
		{
			findpath(dp, a, b, i - 1, j, result);
				
		}
		else if (dp[i][j] == dp[i][j-1])
		{
			findpath(dp, a, b, i, j-1, result);

		}
		else if (dp[i][j] == dp[i - 1][j - 1]+1)
		{
			result.insert(result.begin(),a[i-1]);
			findpath(dp, a, b, i - 1, j - 1, result);
		}
		
	}

};



int main()
{
	Solution s;
	
	string a = "asndffgodfd";
	string b = "nmo";

	cout << "最大公共子序列长度为 " << s.longestCommonSubsequence(a, b) << endl;

	vector<vector<int >> dp = s.longestCommonSubsequenceDp(a, b);
	int row = dp.size();
	int col;
	if (row > 0)
		col = dp[0].size();

	cout << "dp表:" << endl;

	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			cout << dp[i][j] << " ";
		}
		cout << endl;
	}
	
	string path;
	s.findpath(dp, a, b, row - 1, col - 1, path);

	cout << path << endl;

    system("pause");
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值