09 2013 档案
摘要:/* 最优装载问题: 有 n 个集装箱,重量分别为 w[0], ..., w[n - 1] 和一个载重量为 capacity 的货船,现在要把货船尽可能的装满. */#include#include#includeusing namespace std;class maxLoading{ private: size_t n; //貨物个数 size_t capacity; //貨船的載重量 std::vector weight; size_t bestW; size_t currentW; ...
阅读全文
摘要:1. 问题描述设有 n 件物品,其中第 i 件的重量和价值分别为 w[ i ] 和 v[ i ], i = 1, ..., n。现在有一个容量为 V 的背包,要求选择其中的若干件装入背包,使得物品的总重量不超过背包的容量,并且总的价值最大。由于每件物品要么被选要么被不选,因此称为 0-1 背包问题。设 w[ i ],v[ i ],V 均为整数。2. 动态规划解用 opt[ i, j ] 表示用容量为 j 的背包装前 i 件物品的最大价值。则opt[ i, 0 ] = 0, i = 0, 1, ..., n //没有容量了opt[ 0, j ] = 0 //没有东西可装对于i > 0, j
阅读全文
摘要:1. 问题描述 给定一个序列 A[ 1 : n ],求其一个子序列 A[ i : j ],使得从 A[ i ] 到 A[ j ] 的和最大。2. 动态规划解用 opt[ i ] 表示以 A[ i ] 为末尾元素的所有连续子序列和的最大者,则opt[ 1 ] = A[ 1 ]当 i > 1 时:如果 opt[ i - 1 ] 0, 则 opt[ i ] = A[ i ] + opt[ i - 1 ]则结果为 max{ opt[1], opt[2], ..., opt[n] }。时间复杂度为 O(n), 空间复杂度为 O(1)。3. 扩展:首尾相连的情形考虑序列 B = [A1, A2, .
阅读全文
摘要:1. 问题描述有一个序列 A = a[1:n], 若存在一个数列bm,其中对于任何1 int LIS(Iter beg, Iter end, Compare op){ if(beg == end) return 0; int n = end - beg; std::vector opt(n); opt[0] = 1; Iter id = std::next(beg); for(int i = 1; i b ? a : b;}); return length_opt;}templateint LIS(Iter beg, Iter end){ ...
阅读全文
摘要:1. 问题描述2. 动态规划解设 A 和 B 分别为长度为 m 和 n 的序列,用 opt[i, j] 表示 A[1 : i] 与 B[1 : j] 的且末位为 Ai (==Bj) 的公共子串的长度。则opt[0, j] = 0, opt[i, 0] = 0如果 Ai == Bj, opt[i, j] = opt[i - 1, j - 1] + 1如果 Ai!= Bj, opt[i, j] = 03. C++实现// Longest Common Substringtemplatestd::tuple LCSstr(const Iter1 &beg1, const Iter1 &
阅读全文
摘要:1. 问题描述给定序列 X 和 Y,若 S 同时为 X 和 Y 的子序列,则称 S 为 X 和 Y 的公共子序列;若 S 还是 X 和 Y 的所有公共子序列中最长的,则称 S 是 X 和 Y 的一个最长公共子序列。注意区别最长公共子串(要求连续)和最长公共子序列(不要求连续)。最长公共子序列不唯一。2. 动态规划解用 opt[i, j] 表示 X[1 : i] 和 Y[1 : j] 的最长公共子序列的长度。则显然有边界条件:opt[0, j] = 0opt[i, 0] = 0另外还有递归方程:如果 Xi == Yj,则 opt[i, j] == 1 + opt[i - 1, j - 1]如果
阅读全文
摘要:#include#includeusing namespace std;function addn(int n){ auto f = [=](int m){return m + n;}; return f;}int main(){ function f = addn(3); cout << f(1) << endl; cout << f(10) << endl; auto g = addn(12); cout << g(1) << endl; cout << g(10) << endl;}
阅读全文

浙公网安备 33010602011771号