关于动态规划:动态规划01背包LCSLIS

45次阅读

共计 4025 个字符,预计需要花费 11 分钟才能阅读完成。

动静布局的外围是当时找到该事件的动静转移方程,我临时只学习了几个经典的 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 ?

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;
} 

正文完
 0