LIS LCS 模板(转自师哥)

本文介绍最长上升子序列与最长公共子序列的实现方法,通过具体代码示例展示如何运用二分查找优化最长上升子序列的计算效率,并提供最长公共子序列的动态规划解决方案。

 

https://blog.youkuaiyun.com/Akatsuki__Itachi/article/details/76864533

最长上升子序列:

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int MAX=50000;
using namespace std;
int arr[MAX+50],ans[MAX+50],len;
int binary_search(int i)//手写二分法
{
    int left,right,mid;
    left=0,right=len;
    while(left<right)
    {
        mid = left+(right-left)/2;
        if(ans[mid]>=arr[i]) right=mid;
        else left=mid+1;
    }
    return left;
}
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>arr[i];
    ans[0] = arr[1];
    len=0;
    for(int i=2; i<=n; i++)
    {
        if(arr[i]>ans[len])
            ans[++len]=arr[i];
        else
        {
            int pos=binary_search(i);
            //int pos=lower_bound(ans,ans+len,arr[i])-ans;//也可直接使用STL库里的函数
            ans[pos] = min(ans[pos],arr[i]);
        }
    }

    cout<<len+1<<endl;//数组大小就是最长上升子序列的个数

}

 

 

 

最长公共子序列:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int dp[1000][1000];
int main()
{
    string s1,s2;
    while(cin>>s1)
    {
        cin>>s2;
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=s1.size();i++)
        {
            for(int j=1;j<=s2.size();j++)
            {
                if(s1[i-1]==s2[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        cout<<dp[s1.size()][s2.size()]<<endl;
    }
}

 

打印:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define X (sqrt(5)+1)/2.0  //Wythoff
using namespace std;
typedef long long int LL;
const int MAXL(1e3);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
char s1[MAXL+50],s2[MAXL+50];
int dp[MAXL+50][MAXL+50];
int flag[MAXL+50][MAXL+50];
void LCS()
{
    memset(dp,0,sizeof(dp));
    memset(flag,0,sizeof(flag));
    for(int i=1;i<=strlen(s1);i++)
    {
        for(int j=1;j<=strlen(s2);j++)
        {
            if(s1[i-1]==s2[j-1])
                dp[i][j]=dp[i-1][j-1]+1,flag[i][j]=0;
            else if(dp[i-1][j]>=dp[i][j-1])
                dp[i][j]=dp[i-1][j],flag[i][j]=1;
            else
                dp[i][j]=dp[i][j-1],flag[i][j]=-1;
        }
    }
}
void PrintLCS(int i,int j)
{
    if(i==0||j==0)
        return ;
    if(flag[i][j]==0)
    {
        PrintLCS(i-1,j-1);
        cout<<s1[i-1];
    }
    else if(flag[i][j]==1)
        PrintLCS(i-1,j);
    else
        PrintLCS(i,j-1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    while(cin>>s1>>s2)
    {
        LCS();
        PrintLCS(strlen(s1),strlen(s2));
        cout<<endl;
    }
}

LCS LIS 的核心思想是:**将两个序列的最长公共子序列问题化为一个序列的最长递增子序列问题**。但这种换并不是在所有情况下都适用,它有**严格的前提条件**。 --- ## ✅ LCS LIS 的前提条件: ### **条件 1:其中一个序列的元素是 1 到 n 的排列(即所有元素互不重复,且覆盖 1~n)** 也就是说,**该序列中每个数从 1 到 n 各出现一次**。 通常我们选择这个序列作为 `b` 序列,然后: - 将 `b` 中每个元素的位置记录下来 - 把 `a` 中的元素换为它们在 `b` 中的位置 - 然后对这个新序列求 LIS,其长度就是 LCS 的长度 --- ## ✅ 举例说明: ### 给定: ``` a = [1, 7, 3, 5, 9, 4, 8] b = [1, 3, 5, 4, 8, 6, 7] ``` - `b` 是 1~7 的排列(所有元素唯一) - 我们记录每个元素在 `b` 中的位置: ``` pos[1] = 1 pos[3] = 2 pos[5] = 3 pos[4] = 4 pos[8] = 5 pos[6] = 6 pos[7] = 7 ``` - 遍历 `a`,将其中存在于 `b` 的元素替换为它们在 `b` 中的位置: ``` a -> [1, 7, 3, 5, 9, 4, 8] 替换为 -> [1, 7, 2, 3, 无效, 4, 5] 过滤无效后得到:[1, 7, 2, 3, 4, 5] ``` - 对这个新序列求 LIS: ``` LIS = [1, 2, 3, 4, 5] → 长度为 5 ``` → 所以 LCS 的长度也为 5 --- ## ✅ 条件 2:另一个序列可以有重复元素,但我们只取其在 `b` 中出现的部分 也就是说,**只要 `b` 是排列,即使 `a` 中有重复元素,也可以进行换**。 --- ## ✅ 为什么必须是排列? 因为 LIS 要求的是**严格递增**的子序列。 如果我们把 `a` 中的元素映射为它们在 `b` 中的位置,那么: - 只有当这些位置是唯一的、递增的,才能保证对应的是 `b` 中的公共子序列 - 如果 `b` 中有重复元素,那么一个元素可能对应多个位置,无法确定唯一映射,换失败 --- ## ✅ 总结: | 前提条件 | 说明 | |----------|------| | `b` 必须是一个排列 | 所有元素唯一,且为 1~n | | `a` 可以有重复元素 | 但只取在 `b` 中存在的部分 | | 映射后的序列求 LIS | LIS 的长度 = LCS 的长度 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值