高级数据结构04动态规划
1.整体结构

2.实际应用
#include "pch.h"
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int w[] = { 8,6,5,9,7 };
int v[] = { 7,9,6,3,8 };
const int C = 18;
const int SIZE = sizeof(w) / sizeof(w[0]);
int dp[SIZE][C + 1];
void backstrace()
{
int bestv = 0;
int bestx[SIZE] = { 0 };
int c = C;
for (int i = 0; i < SIZE-1; ++i)
{
if (dp[i][c] != dp[i + 1][c])
{
bestv += v[i];
c -= w[i];
bestx[i] = 1;
}
}
if (dp[SIZE - 1][c] > 0)
{
bestv += v[SIZE - 1];
bestx[SIZE - 1] = 1;
}
cout << "max value:" << bestv << endl;
for_each(bestx, bestx + SIZE, [](int a) {
cout << a << " ";
});
}
int main()
{
int n = SIZE - 1;
for (int j = 1; j <= C; ++j)
{
if (w[n] > j)
{
dp[n][j] = 0;
}
else
{
dp[n][j] = v[n];
}
}
for (int i = n - 1; i >= 0; --i)
{
for (int j = 1; j <= C; ++j)
{
if (w[i] > j)
{
dp[i][j] = dp[i + 1][j];
}
else
{
dp[i][j] = max(dp[i + 1][j], dp[i + 1][j - w[i]] + v[i]);
}
}
}
for (int i = 0; i < SIZE; ++i)
{
for_each(dp[i], dp[i] + C + 1, [](int a) {
cout << a << " ";
});
cout << endl;
}
backstrace();
return 0;
}
void backstace(int ar[], int dp[], int end, int max)
{
}
int main05()
{
int max = 0;
int ar[] = { -2,11,-4,13,-5,-2 };
const int c = sizeof(ar) / sizeof(ar[0]);
int dp[c] = { 0 };
dp[0] = ar[0];
if (dp[0] < 0)
{
dp[0] = 0;
}
for (int i = 1; i < c; ++i)
{
dp[i] = dp[i - 1] + ar[i];
if (dp[i] < 0)
{
dp[i] = 0;
}
if (dp[i] > max)
max = dp[i];
}
cout << max << endl;
for_each(dp, dp + c, [](int a) {cout << a << " "; });
backstace(ar, dp, c-1, max);
return 0;
}
int main04()
{
int max = 0;
int ar[] = { 8,3,4,7,2,9 };
int dp[6] = { 0 };
for (int i = 0; i < 6; ++i)
{
dp[i] = 1;
for (int j = 0; j < i; ++j)
{
if (ar[i] >= ar[j] && dp[j] + 1 > dp[i])
{
dp[i] = dp[j] + 1;
}
}
if (max < dp[i])
{
max = dp[i];
}
}
cout << "max lis:" << max << endl;
return 0;
}
int main03()
{
int ar[] = { 1,3,5 };
const int c = 11;
int dp[c + 1] = {0};
for (int i = 1; i <= c; ++i)
{
dp[i] = i;
for (int j = 0; j < sizeof(ar) / sizeof(ar[0]); ++j)
{
if (i >= ar[j] && dp[i - ar[j]] + 1 < dp[i])
{
dp[i] = dp[i - ar[j]] + 1;
}
}
}
cout << "价值11需要的最少的硬币数量是:" << dp[c] << endl;
for_each(dp, dp + c + 1, [](int a) {cout << a << " "; });
return 0;
}
#if 0
int mycount = 0;
const int XSIZE = 10;
const int YSIZE = 6;
int dp[XSIZE][YSIZE];
int path[XSIZE][YSIZE];
int LCS(string x, string y, int m, int n)
{
if (m < 0 || n < 0)
return 0;
if (dp[m][n] >= 0)
{
return dp[m][n];
}
mycount++;
if (x[m] == y[n])
{
dp[m][n] = LCS(x, y, m - 1, n - 1) + 1;
path[m][n] = 1;
}
else
{
int size1 = LCS(x, y, m - 1, n);
int size2 = LCS(x, y, m, n - 1);
if (size1 > size2)
{
dp[m][n] = LCS(x, y, m - 1, n);
path[m][n] = 2;
}
else
{
dp[m][n] = LCS(x, y, m, n - 1);
path[m][n] = 3;
}
}
return dp[m][n];
}
#endif
#if 0
int dp[XSIZE + 1][YSIZE + 1] = {0};
int NON_LCS(string x, string y, int m, int n)
{
for (int i = 1; i <= m+1; ++i)
{
for (int j = 1; j <= n+1; ++j)
{
if (x[i-1] == x[j-1])
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else
{
if (dp[i-1][j] > dp[i][j-1])
{
dp[i][j] = dp[i - 1][j];
}
else
{
dp[i][j] = dp[i][j - 1];
}
}
}
}
return dp[XSIZE][YSIZE];
}
#endif
#if 0
void backstrace(string x, int m, int n)
{
if (m < 0 || n < 0)
return;
if (path[m][n] == 1)
{
backstrace(x, m - 1, n - 1);
cout << x[m] << " ";
}
else
{
if (path[m][n] == 2)
{
backstrace(x, m - 1, n);
}
else
{
backstrace(x, m, n - 1);
}
}
}
#endif
#if 0
int main01()
{
string X = "helloworld";
string Y = "hecdld";
#if 0
for (int i = 0; i < XSIZE; ++i)
{
fill(dp[i], dp[i] + YSIZE, -1);
}
#endif
int size = NON_LCS(X, Y, X.length() - 1, Y.length() - 1);
cout << "size:" << size << endl;
cout << "mycount:" << mycount << endl;
#if 1
for (int i = 0; i < XSIZE; ++i)
{
for_each(dp[i], dp[i] + YSIZE, [](int a) {
cout << a << " ";
});
cout << endl;
}
cout << endl;
#endif
return 0;
}
#endif