#P6401. Broken Phone Keypad
Broken Phone Keypad
Broken Phone Keypad
Marko the grasshopper loves to hop around on the meadow. Unfortunately, his phone fell into a puddle and its keys have become unpredictable. Although each key now behaves like a different key (i.e. when you press a key, it acts as if another key was pressed), no two keys produce the same result. This means that all letters can still be typed, but you have to figure out the new sequence of keypresses.
On a working phone, the digit keys map to letters as follows (the traditional T9 mapping):
- 2: a, b, c
- 3: d, e, f
- 4: g, h, i
- 5: j, k, l
- 6: m, n, o
- 7: p, q, r, s
- 8: t, u, v
- 9: w, x, y, z
To type a letter, you press the corresponding key a number of times equal to the letter's position in that key's group (for example, to type a
you press key 2 once, for b
you press it twice, and for c
three times). If two consecutive letters are mapped to the same key, you must insert a #
between them to separate the keypress sequences.
However, Marko's phone is broken. Instead of the intended key, when you press a physical key the phone registers a different key. You are given the new behavior as a permutation of the digits 2 to 9. Specifically, the first integer in the permutation is the output when physical key 2 is pressed, the second integer is the output for physical key 3, and so on. Since the mapping is one-to-one, you can compute the inverse mapping to determine which physical key to press in order to simulate a certain working key.
Your task is to help Marko by determining the sequence of physical key presses (digits and the '#' character as needed) to type out his message on his broken phone.
The relation between the working phone and the broken phone is given by the following equation in \(\LaTeX\):
[ \text{physical}_\text{key} = f^{-1}(\text{working}_\text{key}) ]
For example, consider a working phone where the mapping is identity (i.e. physical key 2 yields 2, key 3 yields 3, etc.). To type the string klor
, the sequence would be:
55#555666777
because:
k
is the 2nd letter on key 5 (press key 5 twice),l
is the 3rd letter on key 5 (since consecutive, add '#' and then press key 5 three times),o
is the 3rd letter on key 6 (press key 6 three times),r
is the 3rd letter on key 7 (press key 7 three times).
inputFormat
The input consists of two lines:
- The first line contains 8 space‐separated integers. The i-th integer (1 ≤ i ≤ 8) represents the output digit when the physical key corresponding to digit \(i+1\) (i.e. 2 through 9) is pressed. This means that the first integer is the output for physical key 2, the second for key 3, and so on.
- The second line contains a non-empty string consisting of lowercase English letters (a–z) which Marko wants to type.
outputFormat
Output a single line – the sequence of key presses (digits and '#' characters) that Marko needs to press on his broken phone to produce his intended message.
sample
2 3 4 5 6 7 8 9
klor
55#555666777