TopCoder problem "StampPads" used in SRM 158 (Division I Level Two)



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

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=1676

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4598&pm=1676

Writer:

schveiguy

Testers:

lbackstrom , brett1479

Problem categories:

Brute Force