← Back to problems
UNSW COMP2521 · medium

Trie: is it a stored word

Trie: is it a stored word?

Insert n lowercase words into a Trie, then for each of m queries print yes (it is a stored word) or no (it is not).

The crucial, error-prone point: walking the characters to the end of a query only shows it is a prefix of some stored words; whether it is a stored complete word is decided by the final node's isEnd. So the last line of search must be return cur->isEnd; — never return true just because the path exists (that misjudges a prefix such as querying ca when only card is stored).

insert is provided; you only complete search: descend per character, return false on a NULL edge, and return cur->isEnd at the end.

Input

  • Line 1: number of words to insert n.
  • Next n lines: one lowercase word each.
  • Next line: number of queries m.
  • Next m lines: one query word each.

Output

  • m lines, each yes or no.

Example: store car card dog, query car ca card cat -> yes / no / yes / no.