YOUWIN - You Win!

no tags 

You just achieved the High Score on your favorite video game! Now, you get to enter your name! You have to use the controller to enter your name, which can be awkward. Here’s how it works:

  • There are only the 26 capital letters A to Z, in order. There are no numbers, spaces, lower case letters, or any other characters.
  • Pushing UP or DOWN changes the active letter one letter forward (UP) or backward (DOWN). The active letter starts at A. It will not reset when you move around in the name. It also wraps: UP from Z goes to A, DOWN from A goes to Z.
  • Pushing LEFT or RIGHT moves the cursor one letter left or right in the current name. Note that once the cursor is at either end of the current name, it cannot move any further in that direction.
  • Pushing the FIRE button adds the active letter to the name.

For example, consider the name ‘ALMA’. One way you could enter ‘ALMA’ is like this:

This would take 28 button pushes. However, consider entering ‘ALMA’ like this:

This takes only 17 button pushes. Given a name, what is the fewest number of button pushes needed to enter that name? Assume that the active letter starts at A, and that it doesn’t matter where the cursor ends up when you’re done.


There will be several test cases in the input. Each test case will consist of a single string on its own line, with from 1 to 18 capital letters, representing a name that must be entered into the High Score list. The input will end with a line with a single 0.


For each test case, output a single integer representing the smallest number of button pushes needed to enter the name. Output no spaces, and do not separate answers with blank lines.

Example Input


Example Output


hide comments
comp0zr: 2016-08-01 16:25:05

Thank you for the clarification!

darryl: 2016-06-25 16:10:41

your method: A-E (4) + FIRE (1) + LEFT (1) + D-Y (6) + FIRE (1) + X-S (6) + RIGHT*2 (2) + FIRE (1)
remember that after fire, the cursor is automatically moved to the right 1x. so, you just need 1x RIGHT.
no need to run RIGHT*2

comp0zr: 2016-04-22 21:48:14

Hey, thanks for responding. I did try doing it that way, but by my count that equals 24 when you include the "FIRE" command for input: Z-Y [2], FIRE [1], X-W-V-U-T-S [6], FIRE [1], LEFT [1], T-U-V-W-X-Y-Z-A-B-C-D-E [12], FIRE [1] = 24. Anyway, thank you for the help... this doesn't strike me as a difficult problem implementation-wise, if I could just make sense of the sample output!

Last edit: 2016-04-22 21:51:07
darryl: 2016-04-19 03:40:57

have you tried A-Y (2), Y-S (6), LEFT (1), S-E (12)?

comp0zr: 2016-04-11 04:59:20

Any chance you could walk through the process that would allow one to spell "YES" in 21 moves? The lowest I could manage to find was 22, and I'm almost certain I've tried every possible approach. I don't want to start attempting to code this algorithm until I'm positive I fully understand the basic processes behind it. My shortest path through the alphabet was as follows: A-E (4) + FIRE (1) + LEFT (1) + D-Y (6) + FIRE (1) + X-S (6) + RIGHT*2 (2) + FIRE (1) = 22. It's certainly possible that I'm overlooking something completely stupid/obvious as so often is the case... any help would be greatly appreciated. Thanks!

asshole: 2015-10-28 18:28:44

what concept should we use to solve this ?
can i please know

Added by:Joshua Kirstein
Time limit:35s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:ACM SER2013