cswork / OAOpen practice workspace

JPMorgan ChaseOA #26

Get Search Results

EasyJudge-ready

Topics (AI-labelled)HashingStrings

Implement an autocorrect feature: given a word list words and several 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 obtained by rearranging the letters of another string, so every letter must occur exactly the same number of times; a word identical to the query also counts.

Input

The first line contains an integer n, followed by n lines with one word each. Then a line with an integer q, followed by q lines with one query each.

1≤n≤750, 1≤q≤750; every 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, followed by those 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:

7
stop
pots
tops
opts
post
spot
stops
2
post
stops

Output:

6 opts post pots spot stop tops
1 stops

Example 3

Input:

5
aabb
abab
bbaa
ab
abc
3
baba
ba
cab

Output:

3 aabb abab bbaa
1 ab
1 abc

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: stop, pots, tops, opts, post, and spot are all anagrams of one another and are output in lexicographic order; the query post itself is also in the result. stops has one extra s and matches only itself. Example 3: aabb, abab, and bbaa are anagrams of baba; ab matches only ab; cab matches only abc.

Editorial

Loading solution…