google编程挑战赛试题

xiaocai0001 2005-12-12 01:42:28
第一题(250分)
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.
...全文
1767 72 打赏 收藏 转发到动态 举报
写回复
用AI写文章
72 条回复
切换为时间正序
请发表友善的回复…
发表回复
JOKER_UFO 2005-12-14
  • 打赏
  • 举报
回复
学了几年的C++,自以为还可以,今天才晓得什么也不懂,可悲!!!
guyanhun 2005-12-14
  • 打赏
  • 举报
回复
英文也没看懂 ~~
guyanhun 2005-12-14
  • 打赏
  • 举报
回复
感觉自己还差得好远 ~~
StealDream 2005-12-14
  • 打赏
  • 举报
回复
NND,光题目就要整半天
睡在床板下_ 2005-12-14
  • 打赏
  • 举报
回复
(我没练习过)星期一去试了, 一开始很紧张的, 因为是google出的题嘛 ,还装了金山 词霸。。。。。
结果进了平台,就开始搞,250的题目看了 半天才知道啥意思,估计10分钟,也没看完,就开始做了,那是思路好清晰哦,马上就想到算法了,在vc上 调试了,ok没问题, 我就 copy上去,可就是 调试 不通过,,搞了10分钟左右 还是不行,就放弃了,开始750的,那题题目好快看的,马上就懂了,又vc上调试 通过了,又copy上去,调试又通不过。。心想 方正是来试一试的,就没再搞,一直到时间结束 , 后来我问了人才知道,原来不用写 主函数~~~~~
Linux_Web 2005-12-14
  • 打赏
  • 举报
回复
做个记号,MARK
RainWindy 2005-12-13
  • 打赏
  • 举报
回复
我第一题的答案,请高手指点:
#include <vector>
#include <string>

using namespace std;
struct elem
{
int row;
int col;
};

class BusStops
{
private:
int check(const elem& me, const elem& bus_stop, const int& distance);
public:
int countStops(vector <string> cityMap, int walkingDistance);
};

int BusStops::check(const elem& me, const elem& bus_stop, const int& distance)
{
int subrow = me.row - bus_stop.row;
int subcol = me.col - bus_stop.col;

int sub = ((subrow > 0)?subrow:-subrow) + ((subcol > 0)?subcol:-subcol);

if (sub <= distance)
return 0;

return -1;
}

int BusStops::countStops(vector <string> cityMap, int walkingDistance)
{
int ret = 0;
vector <elem> result;
elem me = {-1,-1};
int num = cityMap.size();

for (int i = 0; i < num; i++)
{
string item = cityMap[i];

int size = item.size();

for (int j = 0; j < size; j++)
{
if (item[j] == 'X')
{
me.row = i;
me.col = j;
}
else if (item[j] == 'B')
{
elem tmp = {i, j};

// if (me.row != -1)
// {
// if (check(me, bus_stop) == 0)
// ret++;
// }
// else
result.push_back(tmp);
}
}
}

num = result.size();

for (int k = 0; k < num; k++)
{
if (check(me, result[k], walkingDistance) == 0 )
ret++;
}

return ret;
}
RainWindy 2005-12-13
  • 打赏
  • 举报
回复
有些人几乎做了满分,代码能共享一下吗?
RainWindy 2005-12-13
  • 打赏
  • 举报
回复
楼上的做了多少分呀,我浪费了二十多分钟,是别人告诉我怎样操作的。
zoezinsser 2005-12-13
  • 打赏
  • 举报
回复
和楼主的题目一样!
oo 2005-12-13
  • 打赏
  • 举报
回复
to 回复人: lurenfu(别理我,烦着呢!) ( ) 信誉:100 :

我加了个状态数组,用递归速度也是0.0s
oo 2005-12-13
  • 打赏
  • 举报
回复
有谁做的题目没贴过的,能不能贴出来
oo 2005-12-13
  • 打赏
  • 举报
回复
早上做的,跟RainWindy(风雨交加) 的题目一样
RainWindy 2005-12-13
  • 打赏
  • 举报
回复
看到yelling的题目跟我的第二题一样。
RainWindy 2005-12-13
  • 打赏
  • 举报
回复
奇怪,我昨天做完一题时只剩20分钟,怎么刚才看我第一题提交只花了9:52秒,真是奇怪,我看懂题也不知花了多少时间。
第一题:
Problem Statement
????
You are given a vector <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:
vector <string>, int
Returns:
int
Method signature:
int countStops(vector <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

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.

第二题:
Problem Statement
????
You are given a vector <string> grid representing a rectangular grid of letters. You are also given a string find, a word you are to find within the grid. The starting point may be anywhere in the grid. The path may move up, down, left, right, or diagonally from one letter to the next, and may use letters in the grid more than once, but you may not stay on the same cell twice in a row (see example 6 for clarification).
You are to return an int indicating the number of ways find can be found within the grid. If the result is more than 1,000,000,000, return -1.
Definition
????
Class:
WordPath
Method:
countPaths
Parameters:
vector <string>, string
Returns:
int
Method signature:
int countPaths(vector <string> grid, string find)
(be sure your method is public)
????

Constraints
-
grid will contain between 1 and 50 elements, inclusive.
-
Each element of grid will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
-
Each element of grid will contain the same number of characters.
-
find will contain between 1 and 50 uppercase ('A'-'Z') letters, inclusive.
Examples
0)

????
{"ABC",
"FED",
"GHI"}
"ABCDEFGHI"
Returns: 1
There is only one way to trace this path. Each letter is used exactly once.
1)

????
{"ABC",
"FED",
"GAI"}
"ABCDEA"
Returns: 2
Once we get to the 'E', we can choose one of two directions for the final 'A'.
2)

????
{"ABC",
"DEF",
"GHI"}
"ABCD"
Returns: 0
We can trace a path for "ABC", but there's no way to complete a path to the letter 'D'.
3)

????
{"AA",
"AA"}
"AAAA"
Returns: 108
We can start from any of the four locations. From each location, we can then move in any of the three possible directions for our second letter, and again for the third and fourth letter. 4 * 3 * 3 * 3 = 108.
4)

????
{"ABABA",
"BABAB",
"ABABA",
"BABAB",
"ABABA"}
"ABABABBA"
Returns: 56448
There are a lot of ways to trace this path.
5)

????
{"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA",
"AAAAA"}
"AAAAAAAAAAA"
Returns: -1
There are well over 1,000,000,000 paths that can be traced.
6)

????
{"AB",
"CD"}
"AA"
Returns: 0
Since we can't stay on the same cell, we can't trace the path at all.
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.
aayy 2005-12-13
  • 打赏
  • 举报
回复
小Mark哥
zhouhuahai 2005-12-13
  • 打赏
  • 举报
回复
扔石头过河的题:
#include <iostream>
#include <string>
using namespace std;

class SkipStones
{
public:
int maxDistance(string water)
{
int length = water.size();
int maxl = length/2 + 1;
int sum = 0;

for(int k = maxl; k>=1; k--)
{
int j;

sum = 0;
for(j = k; j>=1; )
{
sum += j;
if(water[sum-1] == 'X' || sum > length)
{
goto xx;
}
if(j != 1)
j=j/2;
else
break;
}
if(j == 1)
return sum;
xx:
NULL;
}
return 0;
}
};
lurenfu 2005-12-13
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class PlayCards {
public:
int maxCards( vector<string> cards );

private:
int maxCards( vector<bool> cards );
};

int PlayCards::maxCards( vector<string> cards )
{
int i;
vector<bool> bCards( 40 );

for ( i = 0; i < 40; i++ ) {
bCards[i] = false;
}

for ( i = 0; i < cards.size(); i++ ) {
int index;
char ch;

if ( cards[i].length() == 3 ) {
ch = cards[i][2];
index = cards[i][0] - '1';
} else {
ch = cards[i][3];
index = 9;
}

switch( ch ) {
case 'S':
break;

case 'H':
index += 10;
break;

case 'D':
index += 20;
break;

case 'C':
index += 30;
break;
}
bCards[index] = true;
}

return maxCards( bCards );
}

int PlayCards::maxCards( vector<bool> cards )
{
int i, j, k, m, n, max = 0, allcard = 0;
vector<bool> tempCard;

// assert( cards.size() == 40 );

for ( i = 0; i < 40; i++ ) {
allcard += cards[i];
}
if ( allcard < 3 ) return 0;

// same value
for ( i = 0; i < 10; i++ ) {
n = cards[i] + cards[10+i] + cards[20+i] + cards[30+i];
tempCard = cards;
if ( n == 3 ) {
tempCard[i] = tempCard[10+i] = tempCard[20+i] = tempCard[30+i] = false;
k = 3 + maxCards( tempCard );
if ( k > max ) max = k;
if ( max == allcard ) return max;
} else if ( n == 4 ) {
tempCard[i] = tempCard[10+i] = tempCard[20+i] = tempCard[30+i] = false;
tempCard[i] = true;
k = 3 + maxCards( tempCard );
if ( k > max ) max = k;
if ( max == allcard ) return max;

tempCard[i] = tempCard[10+i] = tempCard[20+i] = tempCard[30+i] = false;
tempCard[10+i] = true;
k = 3 + maxCards( tempCard );
if ( k > max ) max = k;
if ( max == allcard ) return max;

tempCard[i] = tempCard[10+i] = tempCard[20+i] = tempCard[30+i] = false;
tempCard[20+i] = true;
k = 3 + maxCards( tempCard );
if ( k > max ) max = k;
if ( max == allcard ) return max;

tempCard[i] = tempCard[10+i] = tempCard[20+i] = tempCard[30+i] = false;
k = 4 + maxCards( tempCard );
if ( k > max ) max = k;
if ( max == allcard ) return max;
}
}

// run
for ( i = 0; i < 4; i++ ) {
for ( j = 3; j <= 10; j++ ) {
for ( k = 0; k <= (10-j); k++ ) {
n = 0;
for ( m = 0; m < j; m++ ) {
n += cards[i*10+k+m];
}
if ( n == j ) {
tempCard = cards;
for ( m = 0; m < j; m++ ) {
tempCard[i*10+k+m] = false;
}
int a = j + maxCards( tempCard );
if ( a > max ) max = a;
if ( max == allcard ) return max;
}
}
}
}

return max;
}

int main()
{
vector<string> cards;
cards.push_back( "1 S" );
cards.push_back( "2 S" );
cards.push_back( "3 S" );
cards.push_back( "4 S" );
cards.push_back( "5 S" );
cards.push_back( "6 S" );
cards.push_back( "7 S" );
cards.push_back( "8 S" );
cards.push_back( "9 S" );
cards.push_back( "10 S" );

cards.push_back( "1 H" );
cards.push_back( "2 H" );
cards.push_back( "3 H" );
cards.push_back( "4 H" );
cards.push_back( "5 H" );
cards.push_back( "6 H" );
cards.push_back( "7 H" );
cards.push_back( "8 H" );
cards.push_back( "9 H" );
cards.push_back( "10 H" );

cards.push_back( "1 D" );
cards.push_back( "2 D" );
cards.push_back( "3 D" );
cards.push_back( "4 D" );
cards.push_back( "5 D" );
cards.push_back( "6 D" );
cards.push_back( "7 D" );
cards.push_back( "8 D" );
cards.push_back( "9 D" );
cards.push_back( "10 D" );

cards.push_back( "1 C" );
cards.push_back( "2 C" );
cards.push_back( "3 C" );
cards.push_back( "4 C" );
cards.push_back( "5 C" );
cards.push_back( "6 C" );
cards.push_back( "7 C" );
cards.push_back( "8 C" );
cards.push_back( "9 C" );
cards.push_back( "10 C" );


PlayCards playCards;
cout << playCards.maxCards( cards ) << endl;
return 0;
}
jlu3389 2005-12-13
  • 打赏
  • 举报
回复
40多分做完。到这里放松一下。
lurenfu 2005-12-13
  • 打赏
  • 举报
回复
回复人: oo(为了名副其实,努力学习oo技术ing) ( ) 信誉:110 2005-12-12 22:48:33 得分: 0



最后一题用递归太慢了。对test5在2s内无法完成。


我用的就是递归算法,对于test5,用时0.0秒,top code的环境给出的时间
加载更多回复(51)

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧