Longest Common Ascending Subsequence (LCIS)

Title:

Find the longest common ascending subsequence

answer:

The longest common ascending subsequence = the longest common subsequence (LCS) and the longest ascending subsequence (LIS)
LCS core code:

	for(int i=1;i<=n;i++)
	{
    
    
		for(int j=1;j<=m;j++)
		{
    
    
			if(a[i]==b[j])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
			else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
		}
	}

LIS core code:

	for(int i=1;i<=n;i++)
	{
    
    
		dp[i]=1;
		for(int j=1;j<i;j++)
		{
    
    
			if(a[j]<a[i])dp[i]=max(dp[i],dp[j]+1);
		}
		mx=max(mx,dp[i]);
	}

The code for the longest common ascending subsequence is to find the longest ascending subsequence on the longest common subsequence. That is, on the premise of judging a[i] = = b[j], find the ascending sequence
f[i][j] to represent all a[1 ~ i] and b[1 ~ j] in which b[j ] The set of public ascending subsequences at the end; the value of
f[i][j] is equal to the maximum length of the subsequences of the set; the
complexity O(n 3 )
code:

for (int i = 1; i <= n; i ++ )
{
    
    
    for (int j = 1; j <= n; j ++ )
    {
    
    
        f[i][j] = f[i - 1][j];
        if (a[i] == b[j])
        {
    
    
            int maxv = 1;
            for (int k = 1; k < j; k ++ )
                if (a[i] > b[k])
                    maxv = max(maxv, f[i - 1][k] + 1);
            f[i][j] = max(f[i][j], maxv);
        }
    }
}

Let's take a closer look at the code. The third level of for loop is used to find the length of the longest common ascending subsequence +1 that is less than a[i]. In fact, all we need to know is the longest common ascending subsequence before Length, so that we can use a variable val to store the maximum value in F[ i-1 ][ k ], so that the third layer of loop can be omitted

for (int i = 1; i <= n; i ++ )
    {
    
    
        int maxv = 1;
        for (int j = 1; j <= n; j ++ )
        {
    
    
            f[i][j] = f[i - 1][j];
            if (a[i] == b[j]) f[i][j] = max(f[i][j], maxv);
            if (a[i] > b[j]) maxv = max(maxv, f[i - 1][j] + 1);
        }
    }

We can also reduce the dimension to one dimension.
We know that f[i][j] is derived from f[i-1][j].
We use f[i] to represent the first i elements of a sequence and b sequence ( LCIS) length, t is the position of the end element of the longest LCIS, the new transfer equation:
f[i] = f[t] +1 (a[i] = b[j])
This reduces to one dimension

for(int i=1;i<=n;i++){
    
    
        maxx=0;
        for(int j=1;j<=n;j++){
    
    
            if(b[j]<a[i]&&maxx<f[j]) maxx=f[j];
            if(b[j]==a[i]) f[j]=maxx+1;
        }
    }

Code:

Code for two-dimensional space:
Insert picture description here

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 3010;

int n;
int a[N], b[N];
int f[N][N];

int main()
{
    
    
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i ++ ) scanf("%d", &b[i]);

    for (int i = 1; i <= n; i ++ )
    {
    
    
        int maxv = 1;
        for (int j = 1; j <= n; j ++ )
        {
    
    
            f[i][j] = f[i - 1][j];
            if (a[i] == b[j]) f[i][j] = max(f[i][j], maxv);
            if (a[i] > b[j]) maxv = max(maxv, f[i - 1][j] + 1);
        }
    }

    int res = 0;
    for (int i = 1; i <= n; i ++ ) res = max(res, f[n][i]);
    printf("%d\n", res);

    return 0;
}

Code for one-dimensional array:
Insert picture description here

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#define ll long long
#define maxn 3050
#define inf 2147483647
#define mod 10003
#define eps 1e-6
#define pi acos(-1.0)
#define de(x) ((x)*(x))
using namespace std; 
inline int read(){
    
    
    int x=0,f=1; char ch=getchar();
    while(!isdigit(ch)) {
    
    if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)) {
    
    x=x*10+ch-48;ch=getchar();}
    return x*f;
}
int n,a[maxn],b[maxn],f[maxn],maxx;
signed main(){
    
    
    n=read();
    for(int i=1;i<=n;i++) a[i]=read();
    for(int i=1;i<=n;i++) b[i]=read();
    for(int i=1;i<=n;i++){
    
    
        maxx=0;
        for(int j=1;j<=n;j++){
    
    
            if(b[j]<a[i]&&maxx<f[j]) maxx=f[j];
            if(b[j]==a[i]) f[j]=maxx+1;
        }
    }
    maxx=0;
    for(int i=1;i<=n;i++) if(maxx<f[i]) maxx=f[i];
    printf("%d",maxx);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/114334425