动态规划专题

Posted bijian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了动态规划专题相关的知识,希望对你有一定的参考价值。

一、动态规划

问题:Bone Collector

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?

Input:The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output:One integer per line representing the maximum of the total value (this number will be less than 2 31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

代码:

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

struct BONE{
    int value;
    int weight;
}num[110];

int DP();

int n,v;
int dp[110][110];
int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n>>v;
        for(int i=1;i<=n;i++){
            cin>>num[i].value;
        }
        for(int i=1;i<=n;i++){
            cin>>num[i].weight;
        }
        cout<<DP()<<endl;
    }
    
    return 0;
}

int DP(){
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++){
        for(int j=0;j<=v;j++){
            if(num[i].weight>j){
                dp[i][j] = dp[i-1][j];
            }else{
                dp[i][j] = max(dp[i-1][j],dp[i-1][j-num[i].weight]+num[i].value);
            }
        }
    }
    return dp[n][v];
}

二、最长公共子序列

问题:Common Subsequence

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.

Input:

abcfbc abfcab
programming contest 
abcd mnp

Output:

4
2
0

Sample Input:

abcfbc abfcab
programming contest 
abcd mnp

Sample Output:

4
2
0

代码:

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int dp[100][100];
int main(){
    string s1,s2;
    while(cin>>s1>>s2){
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=(int)s1.size();i++){
            for(int j=1;j<=(int)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;
    }
    return 0;
}

三、最长递增子序列

问题:最少拦截系统

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

Input:输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)
Output:对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.
Sample Input

8 389 207 155 300 299 170 158 65

Sample Output

2

代码:

#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main(){
    int n,temp[100],dp[100];
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>temp[i];
    }
    int ans = -1;
    for(int i=1;i<=n;i++){
        dp[i]=1;
        for(int j=0;j<i;j++){
            if(temp[i]>temp[j]&&dp[j]+1>dp[i]){
                dp[i] = dp[j]+1;
            }
        }
        ans = max(ans,dp[i]);
    }
    cout<<ans<<endl;
    return 0;
}

 

以上是关于动态规划专题的主要内容,如果未能解决你的问题,请参考以下文章

动态规划专题1:斐波拉契数列问题的递归和动态规划

poj 动态规划专题练习

动态规划入门专题合集

算法面试专题-动态规划

#yyds干货盘点# 动态规划专题:小红取数

动态规划专题2:矩阵的最小路径和