头图

动态规划的核心是事先找到该事件的动态转移方程,我暂时只学习了几个经典的dp案例,道阻且长!

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 ?
image.png

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 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14

//(动态规划0/1背包)
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstdio>
#include<queue>
#include<stack> 
#include<set> 
#include<vector> 
using namespace std;
struct Bone{
    int value;
    int vol;
}bone[1010];
int n,v;
int dp[1010][1010];
int ans(){
    memset(dp,0,sizeof(dp));
    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= v; j++){
            if(bone[i].vol > j){
                dp[i][j] = dp[i - 1][j];
            }else{
                dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - bone[i].vol] + bone[i].value);
            }
        }
    }
    return dp[n][v];
}
int main(){
    int t = 0;
    cin >> t;
    while(t--){
        cin >> n >> v;
        for(int i= 1; i <= n; i++){
            cin >> bone[i].value;
        }
        for(int i = 1; i <= n; i++){
            cin >> bone[i].vol;
        }
        cout << ans() << endl;
    }
    return 0;
}

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

//(动态规划最长公共子序列(LCS))
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstdio>
#include<queue>
#include<stack> 
#include<set> 
#include<vector> 
using namespace std;
int dp[1010][1010];
string str1,str2;
int lcs(){
    memset(dp,0,sizeof(dp));
    for(int i = 1; i <= str1.length(); i++){
        for(int j = 1; j <= str2.length(); j++){
            if(str1[i - 1] == str2[j - 1]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }else{
                dp[i][j] = max(dp[i][j - 1],dp[i - 1][j]);
            }
        }
    }
    return dp[str1.length()][str2.length()];
} 
int main(){
    while(cin >> str1 >> str2){
        cout << lcs() << endl;
    }
    return 0;
} 

最少拦截系统

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

//(动态规划最长上升子序列(LIS))
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<cstdio>
#include<queue>
#include<stack> 
#include<set> 
#include<vector> 
using namespace std;
const int MAXN = 10000;
int n;
int a[MAXN];//高度存储数组 
int dp[MAXN];//要用到的系统数 
int lis(){
    int ans = 1;//假定一道dp系统就能完成任务 
    dp[1] = 1;
    for(int i = 2; i <= n; i++){
        int max = 0;//要用到的最大系统数
        for(int j = 1; j < i; j++){
            if(a[j] < a[i] && dp[j] > max){//如果前面的导弹j高度>后面的导弹i高度,则可共用一套系统,此处找他的反例即a[j] < a[i] 
                max = dp[j];//如果不满足降序并且出现新的第的最大值max,则需要加新系统,于是把前一个j的dp[j]所需的系统数赋值给max 
            }
        }
        dp[i] = max + 1;//由于每次i更新max置零,第i个高度的系统(dp[i])每次都会加1,而如果上面的for中发现反例,
        //则此时的max就是除了 第i个高度本身 额外所需的最大系统数,而第i个高度所需的系统数(dp[i])即是前一个高度为j时所需要的系统数(dp[j]即max)+i本身(1) 
        if(ans < dp[i]){//如果所得的所需系统数没有超过ans初值,就输出ans,超过了就更新最大的ans 
            ans = dp[i];
            
        }  
    }
    return ans;
} 
int main(){
    while(cin >> n){
        for(int i = 1; i <= n; i++){
            cin >> a[i];
        }
        cout << lis() << endl; 
    }
    return 0;
} 


沐小轲
9 声望0 粉丝

C++初学者