• POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】


    链接:



    Greatest Common Increasing Subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 2757    Accepted Submission(s): 855


    Problem Description
    This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.
     

    Input
    Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.
     

    Output
    output print L - the length of the greatest common increasing subsequence of both sequences.
     

    Sample Input
    1 5 1 4 2 5 -12 4 -12 1 2 4
     

    Sample Output
    2
     

    Source
     

    Recommend
    lcy



    算法:

     

    LCIS 【最长公共上升子序列分析


    code:

    注意格式 问题:

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    
    const int maxn = 500+50;
    int dp[maxn][maxn];
    int a[maxn],b[maxn];
    int m,n;
    
    
    /****
    求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
    序列下标从 1 开始
    */
    int LCS()
    {
        for(int i = 1; i <= n; i++)
        {
            int tmp = 0; //记录在i确定,且a[i]>b[j]的时候dp[i,j]的最大值
            for(int j = 1; j <= m; j++)
            {
                dp[i][j] = dp[i-1][j];
                if(a[i] > b[j])
                {
                    tmp = dp[i-1][j];
                }
                else if(a[i] == b[j])
                    dp[i][j] = tmp+1;
            }
        }
    //for(int i = 1; i <= m; i++) printf("%d ", dp[n][i]); printf("
    ");
    
    
        int ans = 0;
        for(int i = 1; i <= m; i++)
            ans = max(ans, dp[n][i]);
        return ans;
    
    
    }
    
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            memset(dp,0,sizeof(dp));
    
    
            scanf("%d", &n);
            for(int i = 1; i <= n; i++)
                scanf("%d", &a[i]);
            scanf("%d", &m);
            for(int j = 1; j <= m; j++)
                scanf("%d", &b[j]);
    
    
            printf("%d
    ",LCS());
            if(T != 0) printf("
    ");
        }
    }
    





    内存优化:

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    
    const int maxn = 500+50;
    int dp[maxn];
    int a[maxn],b[maxn];
    int m,n;
    
    /****
    求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
    序列下标从 1 开始
    */
    int LCS()
    {
        for(int i = 1; i <= n; i++)
        {
            int tmp = 0;
            for(int j = 1; j <= m; j++)
            {
                if(a[i] > b[j] && dp[j] > tmp)
                {
                    tmp = dp[j];
                }
                else if(a[i] == b[j])
                    dp[j] = tmp+1;
            }
        }
    
        int ans = 0;
        for(int i = 1; i <= m; i++)
            ans = max(ans, dp[i]);
        return ans;
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            memset(dp,0,sizeof(dp));
    
            scanf("%d", &n);
            for(int i = 1; i <= n; i++)
                scanf("%d", &a[i]);
            scanf("%d", &m);
            for(int j = 1; j <= m; j++)
                scanf("%d", &b[j]);
    
            printf("%d
    ",LCS());
            if(T != 0) printf("
    ");
        }
    }
    

















  • 相关阅读:
    如何增强Linux和Unix服务器系统安全性
    FTP连接不上的解决方法
    PHP获取当前服务器详细信息
    要想提高电脑开机速度首先需要设置这几个功能
    Centos7下安装iptables防火墙
    centos下LVM配置与管理
    基于LNMP环境的ssh2扩展
    60个开发者不容错过的免费资源库
    MySql命令的基本操作
    存储过程 分页【NOT IN】和【>】效率大PK 千万级别数据测试结果
  • 原文地址:https://www.cnblogs.com/freezhan/p/3238960.html
Copyright © 2020-2023  润新知