cswork / OA打开练习工作台

Goldman SachsOA #1

Autocorrect Prototype

简单可在线判题

考点(AI 标注)哈希计数字符串

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.

输入格式

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 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.

样例 1

输入:

4
duel
speed
dule
cars
2
spede
deul

输出:

1 speed
2 duel dule

样例 2

输入:

5
listen
silent
enlist
tinsel
google
3
inlets
google
elgoog

输出:

4 enlist listen silent tinsel
1 google
1 google

样例 3

输入:

5
aab
abb
ab
ba
b
4
aba
bab
ab
b

输出:

1 aab
1 abb
2 ab ba
1 b

样例说明

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.