Ticket machine keyboard
InterviewTime limit1sMemory limit32 MB
Given destination names and a pressed prefix, mark on a 4x8 grid every letter that can immediately follow the prefix in some name.
- Level
Easy3 of 10
- Topics
- String, Simulation, Implementation, Hash map
- Solved
- No attempts yet
Problem
A ticket machine is a device similar to an ATM, installed at train stations to make buying tickets easier. The first step in buying a ticket is choosing the destination. The destination is one of names prepared in advance, and you choose it by pressing the name one letter at a time. Each extra letter reduces the number of destinations that are still possible.
The keyboard on the screen has 4 rows of 8 keys. The letters A to Z sit in order starting from the fourth key of the first row, and the remaining 6 keys are always empty. An empty key and a letter that cannot be pressed are both written as the character *. The initial appearance is shown below.
***ABCDE
FGHIJKLM
NOPQRSTU
VWXYZ***

After each letter the keyboard changes. Only the letters that can be pressed next remain, based on the destinations that are still possible, and every other letter turns into *.
Given destination names and the letters pressed so far, print the appearance of the keyboard just before the next letter is pressed.
Input
The first line contains the number of destinations . ()
Each of the next lines contains one destination name. A name consists of uppercase English letters only and has at most characters. The same name may appear more than once.
The last line contains the string pressed so far. consists of uppercase English letters only, has between and characters, and is a proper prefix of at least one destination name, so a full name is never given.
Output
Print the keyboard as 4 lines of 8 characters. For every name that starts with , keep the letter that follows in its own cell, and print * in every other cell.
A name whose length equals the length of has no next letter, so it keeps no cell.
Hint
In the first example, after ZA is pressed, the third letter is G for a ticket to ZAGREB, D for ZADAR and B for ZABOK.