cswork / OA打开练习工作台

JPMorgan ChaseOA #26

Get Search Results

简单可在线判题

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

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.

输入格式

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

样例 1

输入:

4
duel
speed
dule
cars
2
spede
deul

输出:

1 speed
2 duel dule

样例 2

输入:

7
stop
pots
tops
opts
post
spot
stops
2
post
stops

输出:

6 opts post pots spot stop tops
1 stops

样例 3

输入:

5
aabb
abab
bbaa
ab
abc
3
baba
ba
cab

输出:

3 aabb abab bbaa
1 ab
1 abc

样例说明

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.