众所周知计算机代码底层计算都是0和1的计算,牛牛知道这点之后就想使用0和1创造一个新世界!牛牛现在手里有n个0和m个1,给出牛牛可以创造的x种物品,每种物品都由一个01串表示。牛牛想知道当前手中的0和1可以最多创造出多少种物品。
输入描述: 输入数据包括x+1行:第一行包括三个整数x(2 ≤ x ≤ 20),n(0 ≤ n ≤ 500),m(0 ≤ m ≤ 500),以空格分隔
接下来的x行,每行一个01串item[i],表示第i个物品。每个物品的长度length(1 ≤ length ≤ 50)
输出描述: 输出一个整数,表示牛牛最多能创造多少种物品
输入例子: 3 3 1 1 00 100
输出例子: 2
背包问题
动态规划:
使用dp[i][j][t] 表示给定j个0、
dp[i][j][t]=max⎧⎩⎨dp[i−1][j−array0[i]][t−array1[i]]+1dp[i−1][j][t]
边界:
dp[0][j][t]={10(j>=array0[0]、t>=array1[0])else
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <utility>
#include <set>
using namespace std;
//#define debug_
int x, n, m;
vector<pair<int,int>> vec;
int func()
{
vector<int> tmpvec(m+1);
vector<vector<int>> tmpvec_2(n+1,tmpvec);
vector<vector<vector<int>>> dp(x,tmpvec_2);
for (auto j = 0; j < dp[0].size(); ++j)
{
for (auto t = 0; t < dp[0][j].size(); ++t)
{
if (j >= vec[0].first&&t >= vec[0].second)
{
dp[0][j][t] = 1;
}
}
}
for (auto i = 1; i < x;++i)
{
for (auto j = 0; j < dp[i].size(); ++j)
{
for (auto t = 0; t < dp[i][j].size(); ++t)
{
if (j >= vec[i].first&&t >= vec[i].second)
{
dp[i][j][t] = max(dp[i-1][j-vec[i].first][t-vec[i].second]+1,dp[i-1][j][t]);
}
else
{
dp[i][j][t] = dp[i-1][j][t];
}
}
}
}
return dp[x-1][n][m];
}
int main()
{
#ifdef debug_
x = 3;
n = 0;
m = 100;
vec.push_back(pair<int, int>(2, 0));
vec.push_back(pair<int, int>(3, 0));
vec.push_back(pair<int, int>(4, 0));
#else
cin >> x;
cin >> n;
cin >> m;
vec.resize(x);
string str;
getline(cin, str);
for (auto i = 0; i < x;++i)
{
getline(cin,str);
for (auto j = 0;j<str.size();++j)
{
if (str[j]=='0')
{
vec[i].first++;
}
else
{
vec[i].second++;
}
}
}
#endif
cout<<func();
return 0;
}