Problem 454
Ones and Zeroes
Medium · 20 pts · Array, String, Dynamic Programming
Problem
You are given a list of binary strings and two limits, M and N. Choose as many strings as possible such that, in total, the chosen strings contain at most M zeros and at most N ones. Each string can be chosen at most once. This is a 0/1 knapsack with two capacities: best[z][o] is the most strings that fit using at most z zeros and o ones.
Input
First line: K, M and N. Second line: the K binary strings.
Output
Print the largest number of strings that can be chosen.
Constraints
1 <= K <= 600; 1 <= string length <= 100; 1 <= M, N <= 100
Example input
5 5 3 10 0001 111001 1 0
Example output
4
Explanation
With M = 5 zeros and N = 3 ones, the strings 10, 0001, 1 and 0 together use 5 zeros and 3 ones, so 4 strings can be chosen.