WOBURN CHALLENGE 1995
PEG contra PCS

Problem 4: Wildcard
(ACSL 1981-2, contest #5)

INPUT FILE: test4.in
OUTPUT FILE: test4.out

Wouldn't it be nice to list all of your files that end with the suffix ".PAS", or only those containing the string "TST"? Wouldn't it be nice to retrieve all seven-letter words beginning with "PSY" from an electronic dictionary?

If you haven't guessed by now, you must write a program that will math strings in a list to various patterns. To make the problem interesting, patterns can have the following wildcards:

"?" - matches any single character
"*" - matches any series of 0 or more characters

For example, the pattern "*.PAS" matches all strings ending with the suffix ".PAS" (eg PRIME.PAS, PRINTER.PAS). The pattern "*TST*" matches all strings containing the substring "TST" (eg TSTING.PAS, PRIME.TST, ATSTART.EXE, TST). The pattern "PSY????" matches all seven-letter words with the prfix "PSY" (PSYCHIC, PSYCHED, PSYCHOS).

Write a program that can look for matches to a given pattern which can contain any number of wildcards.

INPUT
The first line contains a number N in the range 1..1000, the number of words in your "matching dictionary".
The next N lines each contain a unique word for your dictionary. These words will contain only the upper-case letters "A".."Z" and periods and be 20 characters or less.
The next 5 lines each contain a pattern which your program should find the matches for. These strings will contain only upper case letters, periods, and the wildcards "?" and "*".

OUTPUT
For each of the 5 search patterns, output a comma-separated list of all the matches found in the dictionary. The results should appear in the same order as given in the dictionary. If no match is found output "NO MATCH".

Sample Input File

6
BELLS
TELLS
FALLS
DOLLS
DULLS
DOLLIES
SELLS
*ELLS
D?LLS
D?LL*
*LL*

Output for Sample Input

NO MATCH
BELLS, TELLS
DOLLS, DULLS
DOLLS, DULLS, DOLLIES
BELLS, TELLS, FALLS, DOLLS, DULLS, DOLLIES
Downloader failed! Response object 006~ASP 0159~Buffering Off~Buffering must be on.