Leave Your Name
Time limit12sMemory limit128 MB
Enter the given uppercase name with the fewest presses of letter-change, cursor-move, and insert buttons.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Bit manipulation, Shortest path
- Solved
- No attempts yet
Problem
Kkung just set the high score on a favorite game! Only the name is left to enter, and the entry pad makes that a chore.
- The only characters available are the uppercase letters A to Z. No digits, no spaces, no lowercase letters, nothing else.
- UP and DOWN change the letter you are about to enter. That letter starts at A, DOWN from A gives Z, and UP from Z gives A.
- LEFT and RIGHT move the cursor by one slot. Once the cursor sits at one end, it cannot move further in that direction.
- FIRE inserts the letter you are about to enter at the cursor. Whatever stood to the right shifts one slot over, and the cursor lands right after the inserted letter.
Say the name is 'ALMA'. Kkung can enter it like this.
That costs presses. Now look at this run instead.
That finishes the same name in presses. Given a name, how few button presses are enough to enter it? The letter to enter starts at A, and the cursor may sit anywhere once the name is complete.
Input
Each test case is one line holding a name made only of uppercase letters, with length between and . The last line of the input holds a single 0.
Output
For each test case, print the smallest number of button presses that enters the name, one per line.