Google Code 2005 一些题目

Balls

Problem Statement
You have several identical balls that you wish to place in several baskets. Each basket has the same maximum capacity. You are given an int baskets, the number of baskets you have. You are given an int capacity, the maximum capacity of each basket. Finally you are given an int balls, the number of balls to sort into baskets. Return the number of ways you can divide the balls into baskets. If this cannot be done without exceeding the capacity of the baskets, return 0.
Each basket is distinct, but all balls are identical. Thus, if you have two balls to place into two baskets, you could have (0, 2), (1, 1), or (2, 0), so there would be three ways to do this.
Definition
Class: FillBaskets
Method: countWays
Parameters: int, int, int
Returns: int
Method signature: int countWays(int  baskets, int capacity, int balls)
(be sure your method is public)

Constraints
-
baskets will be between 1 and 5, inclusive.
-
capacity will be between 1 and 20, inclusive.
-
balls will be between 1 and 100, inclusive.
Examples
0)
2
20
2
Returns: 3
The example from the problem statement.
1)
3
20
1
Returns: 3
We have only 1 ball, so we must choose which of the three baskets to place it in.
2)
3
20
2
Returns: 6
We can place both balls in the same basket (3 ways to do this), or one ball in each of two baskets (3 ways to do this).
3)
1
5
10
Returns: 0
We have more balls than our basket can hold.
4)
4
5
10
Returns: 146
This roblem statement is the exclusive and proprietary property of opCoder, 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.

-------------------------------------------------------------------------------------------
City Map

You are given a String[] cityMap representing the layout of a city. The city consists of blocks. The first element of cityMap represents the first row of blocks, etc. A 'B' character indicates a location where there is a bus stop. There will be exactly one 'X' character, indicating your location. All other characters will be '.'. You are also given an int walkingDistance, which is the maximum distance you are willing to walk to a bus stop. The distance should be calculated as the number of blocks vertically plus the number of blocks horizontally. Return the number of bus stops that are within walking distance of your current location.
Definition:
Class: BusStops

Method: countStops

Parameters: String[], int
Returns: int
Method signature: int countStops(String[] cityMap, int walkingDistance)
(be sure your method is public)
Constraints:
-cityMap will contain between 1 and 50 elements, inclusive.
-Each element of cityMap will contain between 1 and 50 characters, inclusive.
-Each element of cityMap will contain the same number of characters.
-Each character of each element of cityMap will be 'B', 'X', or '.'.
-There will be exactly one 'X' character in cityMap.
-walkingDistance will be between 1 and 100, inclusive.
Examples
0)
{"...B.",
 ".....",
 "..X.B",
 ".....",
 "B...."}
3
Returns: 2
You can reach the bus stop at the top (3 units away), or on the right (2 units away). The one in the lower left is 4 units away, which is too far.
1)
{"B.B..",
 ".....",
 "B....",
 ".....",
 "....X"}
8
Returns: 3
A distance of 8 can get us anywhere on the map, so we can reach all 3 bus stops.
2)
{"BBBBB",
 "BB.BB",
 "B.X.B",
 "BB.BB",
 "BBBBB"}
1
Returns: 0
Plenty of bus stops, but unfortunately we cannot reach any of them.
3)
{"B..B..",
 ".B...B",
 "..B...",
 "..B.X.",
 "B.B.B.",
 ".B.B.B"}
3
Returns: 7 

-------------------------------------------------------------------------------------------
Disk

Problem Statement
You are given a String disk representing the clusters on a disk. An 'X' represents a used cluster, and a '.' represents an available cluster. You are also given an int size representing the size, in clusters, of a file waiting to be written to disk. A file can only be stored in clusters not already being used.
Return the minimum number of groups of consecutive clusters needed to store the file on the disk. (The disk does not wrap around at the end.) Return -1 if the disk does not have enough space available to store the file.
Definition
Class:DiskClusters
Method:minimumFragmentation
Parameters:String, int
Returns:int
Method signature:
int minimumFragmentation(String disk, int size)
(be sure your method is public)
Constraints
-disk will contain between 1 and 50 characters, inclusive.
-Each character of disk will be 'X' or '.'.
-size will be between 1 and 50, inclusive.
Examples
0)
"."
2
Returns: -1
We can't fit the file on the disk.
1)
".XXXXXXXX.XXXXXX.XX.X.X."
6
Returns: 6
There is only ever one cluster together, so all six clusters are separated.
2)
"XX..XX....X.XX........X...X.XX...XXXX..XX...XXXXX."
12
Returns: 2
We fit eight clusters together, and four clusters together.
3)
".X.XXXX.......XX....X.....X............XX.X.....X."
20
Returns: 3
4)
"....X...X..X"
11
Returns: -1
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.

-------------------------------------------------------------------------------------------
Fire Station

Problem Statement
 
You are given a String[], grid, representing a city where each character in grid is a single city block. Each block will contain a digit representing the relative population on that block. For example,a block containing the digit '6' will contain three times as many people as a block containing the digit '2'. You are also given a String[], stations, containing the locations of all the fire stations within the city. Each element of stations is formatted as "r c" (quotes for clarity only), where r and c represent the row and column, respectively, of the block on which a fire station is located. Character j of element i of grid represents the block at row i, column j. All indices are 0-based.
The city has received enough funds to build one additional fire station, and the mayor has decided that it is most important to minimize the average distance between a person and the closest fire station to that person. The metric used to determine the distance between two locations in the city is the Manhattan distance between the two blocks on which the locations are situated. The Manhattan distance between two blocks (r1, c1) and (r2, c2) is |r1-r2|+|c1-c2| (the vertical bars represent absolute value). Determine the block on which the new station should be built and return its row and column formatted as "r c" (quotes for clarity only). The return String should contain no extra leading zeros. If multiple blocks are equally optimal, return the one with the lowest row, and if multiple optimal blocks have the same lowest row, return the one among them with the lowest column. If adding
an additional fire station would not reduce the average distance between a person and the closest fire station to that person, return the empty String ("").
Definition
Class: NewStation
Method: location
Parameters: String[], String[]
Returns: String
Method signature: String location(String[] grid, String[] stations)
(be sure your method is public)

Constraints
-
grid will contain between 1 and 20 elements, inclusive.
-
Each element of grid will contain between 1 and 20 characters, inclusive.
-
Each element of grid will contain exactly the same number of characters.
-
Each element of grid will contain only digits ('0'-'9').
-
At least one character in grid will be non-zero.
-
stations will contain between 1 and 10 elements, inclusive.
-
Each element of stations will be formatted as "r c" (quotes for clarity only), where r and c are each integers between 0 and 19, inclusive, with no leading zeros.
-
Each element of stations will represent a location within the boundaries of grid.

Examples
0)
{"111",
 "111",
 "111"}
{"1 1"}
Returns: "0 1"
There's an existing station at (1, 1) and each block contains exactly the same number of people. Placing a new station at either (0, 1), (1, 0), (1, 2), or (2, 1) would minimize the average distance. (0, 1) is chosen since it has the lowest row. Adding the new station reduces the average distance from approximately 1.33 to 1.0. The distance from each block to the nearest station becomes:
101
101
212

1)
{"111",
 "111",
 "111"}
{"0 0", "0 1", "0 2",
 "1 0", "1 1", "1 2",
 "2 0", "2 1", "2 2"}
Returns: ""
There's a fire station on every block, so adding a new station would not reduce the average distance.
2)
{"2312",
 "0233"}
{"1 3"}
Returns: "0 1"
Placing a fire station at (0, 1) would make the average distance 0.625.


3)
{"2312",
 "0233"}
{"1 1", "1 1"}
Returns: "0 3"


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.

-------------------------------------------------------------------------------------------
Play Card

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 vector <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:
vector <string>
Returns:
int
Method signature:
int maxCards(vector <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.

-------------------------------------------------------------------------------------------
Reversal of the substring

Problem Statement
You are given a String input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.
Definition:
Class: ReverseSubstring
Method: findReversed
Parameters: String
Returns: String
Method signature:
String findReversed(String input)
(be sure your method is public)

Notes
-
The substring and its reversal may overlap partially or completely.
-
The entire original string is itself a valid substring (see example 4).
Constraints
-
input will contain between 1 and 50 characters, inclusive.
-
Each character of input will be an uppercase letter ('A'-'Z').
Examples

0)
"XBCDEFYWFEDCBZ"
Returns: "BCDEF"
We see that the reverse of BCDEF is FEDCB, which appears later in the string.

1)
"XYZ"
Returns: "X"
The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.

2)
"ABCABA"
Returns: "ABA"
The string ABA is a palindrome (it's its own reversal), so it meets the criteria.

3)
"FDASJKUREKJFDFASIREYUFDHSAJYIREWQ"
Returns: "FDF"

4)
"ABCDCBA"
Returns: "ABCDCBA"
Here, the entire string is its own reversal.

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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值