TopCoder problem "LexStringWriter" used in SRM 321 (Division II Level Three)



Problem Statement

    

You have a special machine called a LexStringWriter. It has a display that initially shows the String s, and the cursor's initial position is on the first letter of the string. The machine has three buttons: left, right, and enter. When you press left, the cursor moves one position to the left if possible. When you press right, it moves one position to the right if possible. The width of the display is exactly equal to the length of s, and the cursor can never leave the display. When you press enter, the letter shown at the current cursor position will be printed on paper, and that position on the display will be replaced with a space (' ').

Return the minimal number of button presses necessary to print all of the letters in s in alphabetically order. All occurrences of all letters in s must be printed, so if 'a' appears 3 times, for example, 'a' must be printed 3 times.

 

Definition

    
Class:LexStringWriter
Method:minMoves
Parameters:String
Returns:int
Method signature:int minMoves(String s)
(be sure your method is public)
    
 

Constraints

-s will contain between 1 and 50 characters, inclusive.
-s will contain only lowercase letters ('a'-'z').
 

Examples

0)
    
"aaa"
Returns: 5
Press: enter, right, enter, right, enter.
1)
    
"ba"
Returns: 4
You should print the letter 'a' first.
2)
    
"abba"
Returns: 9
3)
    
"acbbc"
Returns: 12
Print the rightmost letter 'c' before the leftmost.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10001&pm=6771

Writer:

Mike Mirzayanov

Testers:

PabloGilberto , brett1479 , Olexiy , lovro

Problem categories:

Dynamic Programming