Dialling Digits
InterviewTime limit2sMemory limit1024 MB
Given a sorted word list and phone numbers, list for each number the words whose keypad digits spell that number, in alphabetical order.
Problem
Disaster struck at the Billboards And Phone-numbers Company! The hyphen in Phone-numbers is deliberate, used for the backronym.
A database bug erased the mnemonic phrases for every registered phone number. Mnemonic phrases usually appear on billboards, so people who read an advertisement can remember its phone number more easily. To dial a number from a mnemonic phrase, press the number key that carries each letter, as shown in Figure D.1. For example, "1-800-BAPC" is 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 is exactly one word from the word list. The input gives only the part of each phone number that must match a mnemonic phrase, without the "1-800-" prefix or any other prefix.
Figure D.1: The keypad of a telephone, where some number keys carry several letters.
Input
The first line contains two integers and (, ), the number of words and the number of phone numbers.
The next lines each contain a word (), made only of lowercase English letters (a-z). The words are unique and given in alphabetical order.
The last lines each contain a phone number (), made only of digits 2-9 that match the letters of a mnemonic phrase.
Output
For each phone number, output the number of words that are a valid mnemonic phrase for it, followed by those words in alphabetical order.