47. Atomic Spelling

Time Limit: 1 seconds

Memory Limit: 1024 MB

Rating: 1500

Problem Statement

After learning about the elements in Chemistry class, Arnold started to wonder whether it was possible to spell out words with the atomic symbols of some elements. He was surprised to find out that this, indeed, was the case! But this only appeared to work sometimes. More formally, Arnold learned that atomic symbols are always one or two character strings, consisting of uppercase and lowercase Latin letters only. The first letter is always in uppercase and the second letter, if it exists, is always in lowercase. He also says that to "spell out" a word using atomic symbols, one must concatenate the given atomic symbols together to form the word. You may use an atomic symbol multiple times, even if it is given only once. Additionally, you are not required to use all of the given atomic symbols when spelling a word, although you may do so if necessary. For instance, the word $novice$ can be spelled with atomic symbols $No$, $V$, $I$ and $Ce$. The word $pythagoras$ can be spelled with atomic symbols $P$, $Y$, $Th$, $Ag$, $O$, $Ra$ and $S$. Given a list of atomic symbols and words to spell, output "YES" if it is possible to spell using the rules given, and "NO" if not (for each word on separate lines, all letters in output must be in uppercase)

Input

On the first line, you will be given integers $N$ and $M$ $(1 \le N, M \le 1000)$, the number of atomic symbols given and the number of strings to "spell out" respectively. The next $N$ lines each contain string $A_i$, and its length is either $1$ or $2$. The next $M$ lines after that each contain string $S_i$. The length of $S_i$ is a positive integer and does not exceed $1000$.

Output

On the first $M$ lines, output "YES" or "NO" in all uppercase, depending on the answer.

Sample Cases
Sample Input 1:
16 5
N
O
La
P
No
Co
I
Cm
Pu
Te
Er
S
Ti
C
Ce
V
plastic
tesla
edison
novice
computer

Sample Output 1:
YES
YES
NO
YES
NO
Explanation

PLaSTiC TeSLa (Position 1: No such element E or Ed, so it is impossible) NoVICe, NOVICe (Position 3: No such element M or Mp, so it is impossible)

Sources

Test Contest > Problem B

Submit | Back