google中国编程挑战赛-练习题之一(1000分题)
全英文滴~看不太懂啊~~~~55555555555555
Problem Statement
When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line. You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a string where characters of the string represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.
Definition
Class:
CursorPosition
Method:
getPosition
Parameters:
string, int
Returns:
int
Method signature:
int getPosition(string keystrokes, int N)
(be sure your method is public)
Constraints
-
keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive.
-
N will be between 1 and 100, inclusive.
Examples
0)
"ERLLL"
10
Returns: 7
First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7.
1)
"EHHEEHLLLLRRRRRR"
2
Returns: 2
All the right-arrows at the end ensure that we end up at the end of the line.
2)
"ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
10
Returns: 3
3)
"RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
19
Returns: 12
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.
问题点数:20、回复次数:11Top
1 楼jinjazz(近身剪)回复于 2005-12-08 13:39:49 得分 0
题目都看不懂就不要参和勒Top
2 楼weisunding(鼎鼎)回复于 2005-12-08 13:40:54 得分 0
我得了997分 :)Top
3 楼leonchenjian(小黑)回复于 2005-12-08 13:56:26 得分 0
比较简单的一题,1000分的难道是第三题?Top
4 楼feiyun0112(http://feiyun0112.cnblogs.com/)回复于 2005-12-08 14:19:17 得分 5
假设有一个字符串,长度为N,keystrokes是按的键,用来移动光标
要得到光标最后在哪个位置Top
5 楼qwerttyy(今天也要快乐!)(每天回十帖)回复于 2005-12-09 08:50:43 得分 0
weisunding(鼎鼎)
你牛啊。
有题目和你做的答题吗?发上来看看吧。Top
6 楼thunderclap(认识事物都有两面性)回复于 2005-12-09 10:14:20 得分 0
强烈要求weisunding(鼎鼎)发代码。Top
7 楼qwerttyy(今天也要快乐!)(每天回十帖)回复于 2005-12-09 13:23:22 得分 0
得分:878.6
using System;
public class CursorPosition
{
public int getPosition(string keystrokes, int N)
{
int iPos = 0;
char[] cArray = keystrokes.ToCharArray();
for (int i = 0; i < keystrokes.Length; i++)
{
switch (cArray[i])
{
case 'E':
iPos = N;
break;
case 'H':
iPos = 0;
break;
case 'L':
iPos--;
break;
case 'R':
iPos++;
break;
}
if (iPos > N) iPos = N;
else if (iPos < 0) iPos = 0;
}
return iPos;
}
}
Top
8 楼fokker(独孤龙)回复于 2005-12-09 15:01:05 得分 15
using System;
using System.Collections;
public class CursorPosition
{
public int getPosition(string keystrokes, int N)
{
Stack st = new Stack();
int iPos = 1;
for( int i=keystrokes.Length-1; i>=0; i-- )
{
if( keystrokes[i] != 'E' && keystrokes[i] != 'H' )
{
st.Push( keystrokes[i] );
}
else
{
if( keystrokes[i] == 'E' ) iPos = N;
break;
}
}
while( st.Count > 0 )
{
char c = (char)st.Pop();
iPos = addPos( iPos, c, N );
}
return iPos;
}
private int addPos( int iPos, char cOp, int N )
{
if( cOp == 'L' )
{
if( iPos == 1 ) return 1;
iPos--;
}
else
{
if( iPos == N ) return N;
iPos++;
}
return iPos;
}
}Top
9 楼qwerttyy(今天也要快乐!)(每天回十帖)回复于 2005-12-10 08:55:15 得分 0
upTop
10 楼sarrow(阿尔萨斯)回复于 2005-12-10 09:23:27 得分 0
看不懂。Top
11 楼sanjie88(菜鸟依旧,谁动了我的毛片)回复于 2005-12-10 13:34:04 得分 0
题目好像是从网页上拉下来的,Top




