前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >动态规划-LCS、LIS

动态规划-LCS、LIS

作者头像
唔仄lo咚锵
发布2020-09-15 14:44:02
5170
发布2020-09-15 14:44:02
举报

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

#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

#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 删除。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档