cswork / OAOpen practice workspace

Goldman SachsOA #1

Autocorrect Prototype

EasyJudge-ready

Topics (AI-labelled)HashingStrings

Implement an autocorrect feature: given a word list words and a number of queries queries, for each query return all words in the list that are anagrams of it, sorted in ascending lexicographic order.

An anagram is a string formed by rearranging the letters of another string; the count of each letter must be exactly the same. A word identical to the query also counts.

Input

The first line contains an integer n, followed by n lines, each containing one word. The next line contains an integer q, followed by q lines, each containing one query.

1≤n≤750, 1≤q≤750; each word and query has length 1..100 and contains only lowercase English letters; the words in the list are distinct; every query is guaranteed to have at least one anagram in the list.

Output

Output q lines. Line i first gives the number k of results for query i, then the k words in ascending lexicographic order, separated by spaces.

Example 1

Input:

4
duel
speed
dule
cars
2
spede
deul

Output:

1 speed
2 duel dule

Example 2

Input:

5
listen
silent
enlist
tinsel
google
3
inlets
google
elgoog

Output:

4 enlist listen silent tinsel
1 google
1 google

Example 3

Input:

5
aab
abb
ab
ba
b
4
aba
bab
ab
b

Output:

1 aab
1 abb
2 ab ba
1 b

Example notes

Example 1: the only anagram of spede is speed; the anagrams of deul are duel and dule. cars is not an anagram of any query. Example 2: listen, silent, enlist, and tinsel are anagrams of one another and are output in lexicographic order; google and elgoog both map to google. Example 3: aab and abb use the same letters but with different counts, so they are not anagrams; the result for query ab includes the identical word ab.

Editorial

Loading solution…