TopCoder problem "StringProcessor" used in SRM 9 (Division I Level Two , Division II Level Two)



Problem Statement

    
Class Name: StringProcessor
Method Name: processString
Parameters: String,String
Returns: String

Implement a class StringProcessor, which contains a method processString.  The
method takes two Strings as parameters: a String to process and a String of
commands.  The method successively (from left to right) processes the commands
in the command String on the String to process and returns the resulting String.

There is always a pointer pointing to some position in the String to process.
When the method starts, the pointer is pointing to the first character in the
String.  The pointer can point anywhere from the first character to 1 position
to the right of the last character.

The commands are:

0 - move pointer to point to first character. (That's a zero, not a capital
letter oh)

$ - move pointer to the position after the last character.

x - delete the character at the pointer position and shift all characters (if
any) after the position left.  The pointer location does not change.  This
command is processed only if the pointer position is not the position to the
right of the string.

s - swap the character at the pointer position with that to the right of it.
The pointer location does not change.  This command is processed as long as the
pointer is not at the last character or the position to the right of all
characters.

ic - shift all characters at and after the current position right and put the
character 'c' at the pointer location.  Then move the pointer location right so
it points to the same character it was pointing too before the insert (or
points to the right of the String if it pointed to the right of the String
before the insert).  'c' is a variable and can be any allowed character, even a
character that ordinarily is a command.

l - move the pointer right one position.  Processed if the current position is
not to the right of the string.

h - move the pointer left one position.  Processed if the current position is
not the first character.

u - undo the most recent processed command that is not undo, if there is such a
command.  Undo restores the String and pointer location to the state before the
last non-undo command was processed. Remember: Sometimes a command is given,
but not processed (because, for example, the end of the String has been
reached).  'x', 's', 'l', 'h' may be given but not processed.  '0', '$', 'ic'
are always processed when given.

The method signature is:
public String processString(String toProcess, String commands);

toProcess is a String containing letters('a'-'z', 'A'-'Z'), digits('0'-'9'),
spaces(' '), and punctuation ('.',',','?','!').  toProcess will have at least 1
character and at most 50 characters.
commands will be a String of valid commands only.  Any characters following an
'i' (insert) in the String will be letters, digits, spaces, or the punctuation
listed above.  commands will have at most 50 characters.
TopCoder will check that these Strings are valid.

Examples:
-if toProcess="It doesnt get any better than TopCoder." and
commands="lllluxxxxxxxxulllislllxhxi ilioit$hxhi!hs", the resulting String is:
"It gets a lot better than TopCoder!".  Note there is a space between the
second and third 'i' in the command string.

-if toProcess="This is a test." and commands = "0$$uhhhx", the resulting String
is "This is a tet."

-if toProcess="This is a test." and commands = "xhhhhhhhhhhhhhhhhhhhu", the
resulting String is "This is a test."
 

Definition

    
Class:StringProcessor
Method:processString
Parameters:String, String
Returns:String
Method signature:String processString(String param0, String param1)
(be sure your method is public)
    

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=89

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=3008&pm=89

Writer:

Unknown

Testers:

Problem categories: