Google 编程竞赛题
来源:互联网 发布:淘宝手机端图片尺寸 编辑:程序博客网 时间:2024/05/29 19:13
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
我的java实现
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CursorPosition {
//private int n;
//private String keystrokes;
private int Returns;
/**
* @param args
*/
public int getPosition(String keystrokes,int n){
Returns=n;
for (int i=0;i<keystrokes.length();i++){
switch(keystrokes.charAt(i)){
case 'H':
Returns=0;break;
case 'E':
//System.out.println("ok");
Returns=n;break;
case 'L':
if (Returns>0)
Returns--;
break;
case 'R':
if (Returns<n)
Returns++;
break;
default:
//System.out.println(keystrokes.charAt(i));
System.out.println("err:keystrokes");
break;
}
}
return Returns;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String keystrokes;
int n,Returns;
System.out.println("input keystrokes:");
keystrokes=in.readLine();
System.out.println("input N:");
n=Integer.parseInt(in.readLine());
CursorPosition cp=new CursorPosition ();
Returns=cp.getPosition(keystrokes,n);
System.out.println("Returns="+Returns);
}
}
- Google 编程竞赛题
- Google 编程竞赛题
- 东大编程竞赛题
- 一道 Google 竞赛题的解法
- 一道 Google 竞赛题的解法
- Google 竞赛题 SecretSum 的 C++ 解法
- [转贴] 一道Google中国挑战赛竞赛题的解法
- google竞赛题SecretSum的另一种C++解法, 使用递归生成代替循环 -- 1
- google竞赛题SecretSum的另一种C++解法, 使用递归生成代替循环 -- 2
- 一道竞赛题
- 一道竞赛题
- 信息学竞赛题
- csdn编程竞赛题:A、B两个单词通过一个单词集合Dict的最小路径
- 无聊的时候写的,google 竞赛题 SecretSum 的另类C#算法---找符合要求的最小值
- google编程挑战比赛
- Google 新编程语言
- google C++ 编程规范
- google C++ 编程规范
- FTP基础知识
- 关于APE和CUE
- Google 编程竞赛题
- 用JSP实现基于Web的RSS阅读器
- xquery:xml的sql
- Google 编程竞赛题
- OCP证书到了
- 时间服务器(time server)名单
- 国内国外Dns服务器大全
- 校园尴尬事件大全(爆笑)[转帖]
- ASP.Net学习笔记之一
- ASP.NET.2.0.in.C.Sharp.2005.From.Novice.to.Professional的读书笔记[5]
- test
- 多功能DataGrid打印类(WinForm C#)