google中国编程挑战赛题目

来源:互联网 发布:亚马逊大数据分析 编辑:程序博客网 时间:2024/04/28 22:28

1.
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.

2:
Problem Statement
     When a stone is thrown across water, sometimes it will land on the water and bounce rather than falling in right away. Suppose that a stone is thrown a distance of n. On each successive bounce it will travel half the distance as the previous bounce (rounded down to the nearest integer). When it can not travel any further, it falls into the water. If, at any point, the stone lands on an obstruction rather than water, it will not bounce, but will simply deflect and fall into the water. Please look at the figure for further clarification (with black, red and green cells representing banks, obstructions and free water respectively). So, if the stone is thrown a distance 7, it will bounce and travel a distance of 3, then finally a distance of 1, having travelled a total distance of 11 (the green path in the figure). If a stone is thrown a distance of 8, it will reach the opposite bank, and if thrown at distances of 2 or 6 it will hit an obstruction during its travel. These are the three red paths in the figure.

此主题相关图片如下:
按此在新窗口浏览图片
You are given a string water. An 'X' represents an obstruction, while a '.' represents water free from obstruction. You are to return an int representing the maximum distance a stone can travel and finally fall in the water, without hitting any obstructions, and without reaching the opposite bank (going beyond the end of the string). You may choose any initial distance for the throw, which starts from the left side of the string. A distance of 1 is the first character of the string, etc. If no initial throw will result in the stone landing in the water without hitting an obstruction, return 0.
Definition
     Class: SkipStones
Method: maxDistance
Parameters: string
Returns: int
Method signature: int maxDistance(string water)
(be sure your method is public)
    
Notes
- Obstructions are at water level, so the stone will not hit any obstructions while it's in the air.
Constraints
- water will contain between 1 and 50 elements, inclusive.
- Each element of water will contain between 1 and 50 characters, inclusive.
- Each character of each element of water will be 'X' or '.'.
Examples
0)
     "..X.....X..."
Returns: 11
This is the example from the problem statement.
1)
     "...X..."
Returns: 3
If it weren't for the obstruction, we could start with a throw of distance 4, and go a total of 7. But, the best we can do is to throw the stone a distance of 2, and have it skip a distance of 1.
2)
     "....X....X...XXXX.X....."
Returns: 22
12 + 6 + 3 + 1 = 22, is the best case.
3)
     "XXXXXXX.XXX.X.."
Returns: 15
Here, an initial throw of 8 is the only way to avoid hitting an obstruction. Notice that the stone finally falls in the water just before reaching the opposite bank.

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.

原创粉丝点击