Google Code Jam中国挑战赛:入围赛题目2

Problem Statement

    

You are playing a card game, and in your hand, you are holding several cards. Each card has a suit, 'S', 'H', 'D', or 'C', and a value between 1 and 10, inclusive. You may play cards as part of a set, which is three or more cards of the same value, or as part of a run, which is three or more cards of the same suit, in sequential order. (Runs may not wrap, thus, 9-10-1 is not a valid run.) Each card may be played only once.

For example, "1 S", "1 H" and "1 D" would be a valid set. "2 S", "3 S", and "4 S" would be a valid run.

You want to play as many cards as possible, maybe in several plays (see example 4). Given a string[] cards representing the cards held in your hand, you are to return an int indicating the maximum number of cards you can play. Each card will be given in the form "value suit" (quotes added for clarity).

Definition

    
Class: PlayCards
Method: maxCards
Parameters: string[]
Returns: int
Method signature: int maxCards(string[] cards)
(be sure your method is public)
    

Constraints

- cards will contain between 0 and 20 elements, inclusive.
- No two elements of cards will be the same.
- Each element of cards will be of the form "value suit" (quotes added for clarity).
- Each number represented will be between 1 and 10, inclusive, with no leading zeroes.
- Each suit represented will be 'S', 'H', 'D', or 'C'.

Examples

0)
    
{"1 S", "2 S", "3 S"}
Returns: 3
We have a run of three cards, which we can play.
1)
    
{"4 C", "4 D", "4 S", "3 S", "2 S"}
Returns: 3
We can take the 4's as a set, or we can take the 2-3-4 run. Either way, we play 3 cards.
2)
    
{"1 S", "2 S", "2 H", "3 H", "3 D", "4 D", "4 C", "5 C", "5 S"}
Returns: 0
We've got lots of cards, but no way to put three together.
3)
    
{"1 S", "2 S"}
Returns: 0
Since we have to play at least three cards at a time, there's nothing to do here.
4)
    
{"1 S", "2 S", "10 S", "5 S", "8 S",
 "3 H", "9 H", "6 H", "5 H", "4 H",
 "10 D", "5 D", "7 D", "4 D", "1 D",
 "2 C", "4 C", "5 C", "6 C", "7 C"}
Returns: 9
The best we can do is to take the set of 4s, the 5-6-7 C, and the remaining three 5s. We could have taken the 4-5-6-7 of C, or all four 5s, but we would not end up playing as many cards.

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.

原文地址:https://www.cnblogs.com/whitewin/p/295767.html