Keyboard Solo
Time limit2sMemory limit1024 MB
Given a typed word with at most one keyboard mistake, list the dictionary words that could have produced it, in lexicographic order, up to five.
- Level
Medium6 of 10
- Topics
- Hash map, String, String matching
- Solved
- No attempts yet
Problem
Staff at the Ministry of Fast Printing and Long Strings (MBPDS) are worried about a new problem. Recently the texts they type have become harder, and they make more mistakes. Fortunately, MBPDS staff are well trained, so each worker makes at most one mistake in any word.
The possible mistakes are:
- a neighboring key is pressed instead of the needed key;
- the needed key is not pressed;
- one of the neighboring keys is pressed before the needed key;
- one of the neighboring keys is pressed after the needed key.
Six keys count as neighbors of a key: left, right, upper left, upper right, lower left, and lower right. The keyboard is a standard QWERTY keyboard, shown in the figure.
The Ministry wants to automate error correction. Write a program that, given a word with at most one mistake, prints the dictionary words that could have been typed.
Input
The first line contains (), the number of words in the dictionary. Each of the next lines contains one dictionary word. All dictionary words are distinct.
The next line contains (), the number of words to check. Each of the next lines contains one word to check.
All words consist of uppercase Latin letters. The input file has no empty lines and is at most one megabyte.
Output
For each word to correct, print a block of one or more lines. The first line of the block is , the number of dictionary words that could match the typed word. Then print those dictionary words in lexicographic order, one per line. If , print only the first five words.