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.


我的答案:


#include <string>
#include <iostream>
using namespace std;
class CursorPosition
{
public:
    int getPosition(string keystrokes,int N);
};

int CursorPosition::getPosition(string keystrokes,int N)
{
    int pos = 0,i;
    int nCmd = keystrokes.size();
    int ELast = keystrokes.find_last_of(‘E’);
    ELast = ELast!=string::npos?ELast:0;
    int HLast = keystrokes.find_last_of(‘H’);
    HLast = ELast!=string::npos?HLast:0;
    pos = ELast>HLast?N:0;
   
    for(i= ELast>HLast?ELast:HLast;i<nCmd;i++)
    {
        switch(keystrokes[i])
        {
        case ‘L’:
            pos–;
            break;
        case ‘R’:
            pos++;
            break;
        }
        pos = pos>N?N:pos;
        pos = pos<0?0:pos;
       
    }
    return pos;
}

int main(int argc,char *argv[])
{
    CursorPosition cp;
    cout<<cp.getPosition("RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL",19)<<endl;
    return 0;
}


评论

该日志第一篇评论

发表评论

评论也有版权!