• CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径


    推荐一篇炒鸡赞的blog。

    以下代码中有打印路径。

    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <queue>
    #include <cmath>
    #include <stack>
    #include <map>
    #include <ctime>
    #include <iomanip>
    
    #pragma comment(linker, "/STACK:1024000000");
    #define EPS (1e-6)
    #define LL long long
    #define ULL unsigned long long
    #define _LL __int64
    #define INF 0x3f3f3f3f
    #define Mod 1000000007
    
    using namespace std;
    
    int A[510],B[510];
    
    int py[510][510],px[510][510],dp[510][510] = {0};
    
    void Output(int x,int y)
    {
        if(x == -1 && y == -1)
            return ;
    
        Output(px[x][y],py[x][y]);
    
        if(A[x] == B[y])
            printf("%d ",A[x]);
    }
    
    int main()
    {
        int n,m,i,j,mlen,x,y;
    
        scanf("%d",&n);
        for(i = 1;i <= n; ++i)
            scanf("%d",&A[i]);
    
        scanf("%d",&m);
        for(i = 1;i <= m; ++i)
            scanf("%d",&B[i]);
    
        int Max = 0,ax,ay;
    
        for(i = 1;i <= n; ++i)
        {
            mlen = 0,x = -1,y = -1;
            for(j = 1;j <= m; ++j)
            {
                dp[i][j] = dp[i-1][j];
                px[i][j] = i-1,py[i][j] = j;
                if(B[j] < A[i] && dp[i-1][j] > mlen)
                    mlen = dp[i-1][j],x = i-1,y = j;
    
                if(B[j] == A[i])
                    dp[i][j] = mlen + 1,px[i][j] = x,py[i][j] = y;
                if(dp[i][j] > Max)
                    Max = dp[i][j],ax = i,ay = j;
            }
        }
    
        if(Max)
            printf("%d
    ",Max),Output(ax,ay);
        else
            printf("0
    ");
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  • 相关阅读:
    Leon-ai on WSL
    自动化测试工具
    创建自己的Spring Boot Starter
    Spring Boot内嵌Tomcat session超时问题
    Spring Boot
    Spring Cloud
    Socket编程之Tomcat模拟_采坑汇总
    访问者模式
    模版模式
    策略模式
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5238084.html
Copyright © 2020-2023  润新知