Type with a joystick

3

37 votes
Graphs, Algorithms, Graph Representation
Problem

You are required to type using a joystick instead of a keyboard. To type using a joystick at the first, you need a keymap. A keymap maps every lowercase English character to a point in 2D dimension space in which x,y are integers. You must never map two distinct characters to the same point. Also, you require a pointer that is a point in 2D dimension space.

Now, you can start typing. At first, you can position the pointer freely where you want. Then, you start typing string s character by character. Everything is normal, you can move the pointer down (x,y1), up (x,y+1), left (x1,y), and, right (x+1,y).

If you position the pointer on a point where a character is mapped to it, you can insert the character using the red button on the joystick.

Your task is to minimize the total number of moves (inserting character does not count as a move). It's easy to inform that the moves are obviously determined if the keymap is given. print a keymap that minimizes the cost if you are given s

Input format

The first line contains s.

Output format

Print 26 lines. In the ith line, print x,y denoting the coordinates of the mapped point to the ith English lowercase character.

Constraints

|S|=106

S consists of lowercase English characters.

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Note that the sample doesn't follow the constraints. The number of needed moves with the above keymap is 52. Instead of the keymap above, we can print one of the optimal mappings that is:

  • s: 0, 0
  • a: 0, 1
  • l: 0, 2
  • m: 1, 1

And other characters are not important. Using the mapping above we can reduce the number of moves to 1+1+1+1 = 4 which is the minimum possible.

Editor Image

?