Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >动态规划-LCS、LIS

动态规划-LCS、LIS

作者头像
唔仄lo咚锵
发布于 2020-09-15 06:44:02
发布于 2020-09-15 06:44:02
56300
代码可运行
举报
运行总次数:0
代码可运行

LCS(Longest Common Subsequence)最长公共子序列。给定两个序列a和b,当另一序列c即是a的子序列又是b的子序列时,称c时a和b的公共子序列,最长公共子序列时所有子序列中长度最长的。 注意子序列是在原序列中删去若干元素后得到的序列,注意子序列≠子串,子串在原序列中是连续的。

​​

HDU-1159 Common Subsequence

Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line. Sample Input abcfbc abfcab programming contest abcd mnp Sample Output 4 2 0

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
char a[maxn], b[maxn];
int dp[maxn][maxn];
int main() {
    while (~scanf("%s%s", a + , b + )) {
        memset(dp, , sizeof(dp));
        int len1 = strlen(a + );
        int len2 = strlen(b + );
        for (int i = ; i <= len1; i++)
            for (int j = ; j <= len2; j++)
                if (a[i] == b[j])dp[i][j] = dp[i - ][j - ] + ;
                else dp[i][j] = max(dp[i][j - ], dp[i - ][j]);
        printf("%d\n", dp[len1][len2]);
    }
    return ;
}
//滚动数组版后文有

high[]

d[]

len

1 5 6 2 3 4

1

1

1 5 6 2 3 4

1 5

2

1 5 6 2 3 4

1 5 6

3

1 5 6 2 3 4

1 2 6

3

1 5 6 2 3 4

1 2 3

3

1 5 6 2 3 4

1 2 3 4

4

结束后len=4,即LIS长度为4。

HDU-1257 最少拦截系统

Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹. 怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统. Input 输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔) Output 对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统. Sample Input 8 389 207 155 300 299 170 158 65 Sample Output 2

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int n, high[maxn];
int LIS1() {  //法一
    int high2[maxn],dp[][maxn] = {  };//滚动数组
    memcpy(high2, high, sizeof(high));
    sort(high2 + , high2 + n + );
    for (int i = ; i <= n; i++) //LCS
        for (int j = ; j <= n; j++)
            if (high[i] == high2[j])
                dp[i & ][j] = dp[i &  ?  : ][j - ] + ;
            else
                dp[i & ][j] = max(dp[i &  ?  : ][j], dp[i & ][j - ]);
    return dp[n&][n];
}
int LIS2() {  //法二
    int dp[maxn], ans = ;
    for (int i = ; i <= n; i++) {
        dp[i] = ;
        for (int j = ; j < i; j++)
            if (high[j] < high[i])
                dp[i] = max(dp[j] + , dp[i]);
        ans = max(ans, dp[i]);
    }
    return ans;
}
int LIS3() {  //法三
    int d[maxn], len = ;
    d[] = high[];
    for (int i = ; i <= n; i++)
        if (high[i] > d[len])
            d[++len] = high[i];
        else {  //用lower_bound()查询第一个>high[i]的地址
            int j = lower_bound(d + , d + len + , high[i]) - d;
            d[j] = high[i];
        }
    return len;
}
int main() {
    while (cin>>n) {
        for (int i = ; i <= n; i++)
            cin >> high[i];
        //cout << LIS1() << "\n";
        //cout << LIS2() << "\n";
        cout << LIS3() << "\n";
    }
    return ;
}

原创不易,请勿转载本不富裕的访问量雪上加霜 ) 博主首页:https://blog.csdn.net/qq_45034708

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HDU 1159.Common Subsequence【动态规划DP】
Problem Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y. The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
风骨散人Chiam
2020/10/28
2940
HDU 1159.Common Subsequence【动态规划DP】
动态规划 导弹拦截
本文讨论了导弹拦截系统,第一问是计算能拦截多少导弹,第二问是计算最少要几套系统。第一问通过不断改变终止点的位置来更新dp数组,最终得出最多能拦截多少导弹。第二问也是通过不断改变终止点的位置来更新dp数组,但需要考虑最坏的情况,即导弹越来越小,因此需要将dp[0]初始化为1,其他位置初始化为0,最终得出最少要几套系统。
Kindear
2018/01/03
9910
动态规划专题刷题记录②:最长上升子序列
朴素的LIS做法,这里展示O(n^2)的做法,思考方法见上方最长上升子序列模型的思考方法。
Here_SDUT
2022/06/29
1.1K0
动态规划专题刷题记录②:最长上升子序列
poj-------Common Subsequence(poj 1458)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 34477 Accepted: 13631 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2
Gxjun
2018/03/21
6450
动态规划专题——线性DP
给定一个如下图所示的数字三角形,从顶部出发,在每一结点可以选择移动至其左下方的结点或移动至其右下方的结点,一直走到底层,要求找出一条路径,使路径上的数字的和最大。
浪漫主义狗
2022/09/21
5920
LCS、LIS、LICS算法
给定两个序列 ,设 为 的长度,其中 分别表示 从首元素到第 i 个元素的一段、 从首元素到第 个元素的一段, 分别表示 中第 i个元素、 中第 个元素,序列 和 的长度分别为 和 。则 的状态转移方程为:
hotarugali
2022/03/01
8500
试题 算法训练 拦截导弹
资源限制 内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s 问题描述   某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
GeekLiHua
2025/01/21
830
ACM 省赛E题 最长的递增子序列(动态规划+最长递增子序列)--------C语言—菜鸟级
最长的递增子序列 Bobo学会了如何计算ICPCCamp中O(nlogn)中的最长增加子序列(LIS)。 对于那些没有加入ICPCCamp的人来说,召回LIS(a1,a2,…,an)被定义为f [
Fivecc
2022/11/21
4550
POJ-1458 Common Subsequence(线性动规,最长公共子序列问题)
Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44464 Accepted: 18186 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1
ShenduCC
2018/04/25
9130
算法训练 拦截导弹
  某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。   输入导弹依次飞来的高度(雷达给出的高度数据是不大于30000的正整数),计算这套系统最多能拦截多少导弹,如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
刘开心_1266679
2019/02/14
8520
人工智能基础-动态规划
田忌赛马中,使用下等马对战上等马,使用上等马和中等马对战中等马和下等马,这就是运筹学的一个应用
DearXuan
2022/01/19
4040
hdu----(1257)最少拦截系统(dp/LIS)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19172    Accepted Submission(s): 7600 Problem Description 某 国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能 超过前一发的高度
Gxjun
2018/03/26
8740
POJ1458(最长公共子序列)
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, …, xm > another sequence Z = < z1, z2, …, zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, …, ik > of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
AI那点小事
2020/04/20
3460
线性DP总结(LIS,LCS,LCIS,最长子段和)
做了一段时间的线性dp的题目是时候做一个总结 线性动态规划无非就是在一个数组上搞嘛, 首先看一个最简单的问题: 一,最长字段和 下面为状态转移方程 for(int i=2;i<=n;i++) { if(dp[i-1]>=0) dp[i]=dp[i-1]+a[i]; else dp[i]=a[i]; } 例题
ShenduCC
2018/04/26
1.6K0
【codevs1044】导弹拦截问题与Dilworth定理
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
饶文津
2020/05/31
1.1K0
1044 拦截导弹 1999年NOIP全国联赛提高组 个人博客:attack.cf
1044 拦截导弹 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description     某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。 输入描述 Input Description 输入
attack
2018/04/13
9380
【动态规划】最长公共子序列
For given two sequences X and Y, a sequence Z is a common subsequence of X and Y if Z is a subsequence of both X and Y. For example, if X={a,b,c,b,d,a,b} and Y={b,d,c,a,b,a}, the sequence {b,c,a}is a common subsequence of both X and Y. On the other hand, the sequence {b,c,a} is not a longest common subsequence (LCS) of X and Y, since it has length 3 and the sequence {b,c,b,a}, which is also common to both X and Y, has length 4. The sequence {b,c,b,a} is an LCS of X and Y, since there is no common subsequence of length 5 or greater.
灯珑LoGin
2022/10/31
3380
【题解】Gym – 102307C Common Subsequence
题目大意就是给出两个序列,找他们的最长公共子序列,然后判断这个子序列的长度是否大于原序列的0.99。
灯珑LoGin
2022/10/31
1550
DP专题 | LIS POJ - 2533
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK<= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
ACM算法日常
2019/06/17
4100
最长上升子序列(LIS)算法
LIS(Longest Increasing Subsequence)最长上升子序列 一个数的序列bi,当b1 < b2 < … < bS的时候,我们称这个序列是上升的。
全栈程序员站长
2022/06/28
9710
相关推荐
HDU 1159.Common Subsequence【动态规划DP】
更多 >
领券
💥开发者 MCP广场重磅上线!
精选全网热门MCP server,让你的AI更好用 🚀
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档