SRM 158 Division-I, Level 2

Problem Statement
   
Note to plugin users: there is an image in this problem statement. Please view the statement in the applet to see the image
There is a collection of multi-colored stamp pads at the local craft store. Each pad has 5 colors on it, arranged as pie wedges (see picture). The wedges can be switched out with other wedges, so you can create the ultimate blend of colors for your favorite stamp. You have a wish list of certain colors, but each pad set is expensive, so you want to minimize the cost. Given the colors of each pad and the colors you want, return the minimum number of pad sets that you must buy in order to get the right colors.
Here is an example of a stamp pad that you can buy:
 
You will be given a string[] pads, and a string[] wishlist. Each element in pads represents a stamp pad with 5 colors on it. Each pad will be in the format:
"<color> <color> <color> <color> <color>"
Each <color> will be a string of lower case letters 'a' - 'z', and the colors will be separated by single spaces. For example, the above stamp pad would be represented by the string:
"yellow red purple blue cyan"
Each element of wishlist is a color that you wish to own. Your method should return the minimum number of pads you must buy to get all the colors in wishlist, or -1 if it is not possible to do.
Definition
   
Class:
StampPads
Method:
bestCombo
Parameters:
string[], string[]
Returns:
int
Method signature:
int bestCombo(string[] pads, string[] wishlist)
(be sure your method is public)
   

Constraints
-
pads will have between 1 and 20 elements, inclusive.
-
Each element in pads will have between 9 and 50 characters, inclusive.
-
Each element in pads will consist of exactly 5 color names separated by single spaces.
-
Each color name in pads will have between 1 and 15 characters, inclusive, and will consist of only lowercase letters 'a'-'z', inclusive.
-
There will be no repeated color names in a single element of pads
-
wishlist will have between 1 and 25 elements, inclusive.
-
Each element of wishlist will have between 1 and 15 characters, inclusive, and will consist of only lowercase letters 'a'-'z', inclusive.
-
There will be no repeated elements in wishlist.
Examples
0)

   
{"yellow red purple blue cyan",
 "red green orange magenta yellow",
 "brown black orange yellow tan"}
{"orange", "yellow", "red", "blue", "magenta", "tan"}
Returns: 3
We can only get blue from the first pad, magenta from the second pad, and tan from the last. Therefore, all three must be purchased.
1)

   
{"yellow red purple blue cyan",
 "red green orange magenta yellow",
 "brown black orange yellow tan"}
{"orange", "yellow", "red", "blue", "tan"}
Returns: 2
Although we can get orange and yellow from the second pad, we can get orange, yellow, and tan from the last, so the second is unnecessary.
2)

   
{"yellow black blue green red",
 "yellow brown cyan magenta tan",
 "black grey fire maroon silver",
 "blue white neon tangerine rust",
 "green orange soot turquoise mint",
 "red cream opal chrome sky"}
{"yellow", "black", "blue", "green", "red",
 "brown", "grey", "white", "orange", "cream"}
Returns: 5
Although you can buy the first pad and get 5 colors on the wishlist, you still must buy the other 5 to get the rest of the colors. However, if you just buy the last 5 pads, you get all the colors.
3)

   
{"red green orange magenta yellow"}
{"silver"}
Returns: -1
The single stamp pad available does not have silver, so the wishlist is impossible to fulfill.
4)

   
{"a i y d o", "t s k g e", "j u w i k", "u k l s j", "q s a c y",
 "q m d x a", "m s l h r", "s x q l n", "u r j s k", "e w v d p",
 "o l a b q", "f z g a m", "o g k b a", "c h g k t", "z v s n x",
 "z n b w c", "h p o u k", "t z o x m", "a w i v z", "u t v m y"}
{"x", "b", "u", "c", "h", "j", "t", "v", "d", "g",
 "k", "w", "y", "z", "a", "i", "m", "l", "n", "e"}
Returns: 6

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

At first glance, one might be inclined to do some sort of optimization, or use a greedy solution. But with only 20 stamp pads, the brute force solution will only take 2^20 or 1 million iterations, executing in well under 8 seconds. One method is to loop from 1 to 2^pads.length and use each bit in the mask to determine whether a pad is included or not. The second is to use a recursive function, deciding on whether to include a pad or not, and recursivly calling for the next pad.

The second part of the solution is to determine which colors are satisfied by the stamps. Since we only care about at most 25 colors, we can use a bitmask to represent the colors from wishlist that have been purchased. If, after setting all the bits for the colors purchased, the bitmask includes all the bits in the wishlist, we minimize a pad count. If no combinations set all the bits, then we must return -1.


你可能感兴趣的:(visio)