Under construction

Still being built. Things may change

Hangman Game

Time limit1sMemory limit128 MB

Summary
Given a hidden word, find the order to select each distinct letter starting from A on a circular alphabet dial that minimizes total LEFT/RIGHT/OK button presses.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, String, Simulation
Solved
No attempts yet

Problem

Juyeon is playing a hangman game. The goal is to guess every alphabet letter that appears in a hidden word.

At first, every alphabet letter in the word is covered by -. When Juyeon chooses a letter, every position containing that letter is revealed. The game ends when all letters appearing in the word have been guessed.

Juyeon can identify the word just from its length and the positions of spaces. Therefore, it is enough to decide the order in which to choose the distinct letters that appear in the word. Juyeon wants to finish the game with as few button presses as possible.

There are three buttons for choosing letters: LEFT, RIGHT, and OK.

  • Exactly one letter is always shown on the screen. Initially, the shown letter is A.
  • Pressing OK chooses the letter currently shown on the screen. Every matching position in the hidden word is revealed. The shown letter itself does not change.
  • Pressing LEFT changes the shown letter to the previous letter. For example, C becomes B, and A becomes Z.
  • Pressing RIGHT changes the shown letter to the next letter. For example, B becomes C, and Z becomes A.

Find a way to finish the game using the minimum possible number of button presses. Output the order in which letters are revealed in such a way. If there are multiple valid answers, output any one of them.

Input

The first line contains the hidden word. Its length is between 1 and 100, inclusive, and it consists only of uppercase English letters and spaces. The first and last characters are not spaces, and there is exactly one space between adjacent words.

Output

On the first line, output the minimum number of button presses needed to finish the game.

On the second line, output the order in which the letters are revealed when using such an optimal sequence of button presses.

Hint

The button count includes both LEFT/RIGHT presses used to move between letters and OK presses used to choose letters. If the same letter appears more than once, choosing it once reveals all of its positions.

Examples3

  1. Example 1

    Input
    TURBO
    
    Expected output
    19
    BUTRO
    
  2. Example 2

    Input
    PUTRA ZA SUTRA
    
    Expected output
    18
    AZUTSRP
    
  3. Example 3

    Input
    IGRA BEZ GRANICA 
    
    Expected output
    28
    AZBCEGINR