Problem D
Dialling Digits

Disaster struck at the Billboards And Phone-numbers Company! Due to a bug in their database system, they lost the corresponding mnemonic phrases for each of the registered phone numbers. These mnemonic phrases are typically used on billboards, to make a phone number for an advertisement easier to remember by people who read them. To dial the phone number from a mnemonic phrase, you simply have to press the number keys corresponding to each letter, as shown in Figure 1. For example, the phone number “1-800-BAPC” would be dialled as “1-800-2272”.
Using this information and a given list of valid words, reconstruct the possible mnemonic phrases for each registered phone number. Each mnemonic phrase consists of exactly one word from the word list. In the input, you will only receive the part of the phone number that should be exactly linked to possible mnemonic phrases, so it does not include the “1-800-” prefix (or any other prefix).
![\includegraphics[width=0.45\textwidth ]{keypad-simplified.pdf}](/problems/diallingdigits/file/statement/en/img-0002.png)
Input
The input consists of:
-
One line with two integers $n$ and $m$ ($1 \leq n, m \leq 10^5$, $n \cdot m \leq 10^5$), the number of words and the number of phone numbers.
-
$n$ lines, each with a word $w$ ($1 \leq |w| \leq 10$), consisting only of English lowercase letters (a-z). The words are unique and given in alphabetical order.
-
$m$ lines, each with a phone number $p$ ($1 \leq |p| \leq 10$), consisting only of digits that correspond to letters in a mnemonic phrase (2-9).
Output
For each phone number, output the number of words that are a valid mnemonic phrase for this phone number, followed by these words in alphabetical order.
Sample Input 1 | Sample Output 1 |
---|---|
5 3 algorithm bapc benelux contest progaming 2272 424242 2363589 |
1 bapc 0 1 benelux |
Sample Input 2 | Sample Output 2 |
---|---|
3 1 peer reds refs 7337 |
3 peer reds refs |
Sample Input 3 | Sample Output 3 |
---|---|
7 3 black judge my of quartz sphinx vow 25225 782789 774466 |
1 black 1 quartz 0 |
Sample Input 4 | Sample Output 4 |
---|---|
5 1 and bland e land of 63 |
1 of |