#K92292. Word Formation Challenge

    ID: 38165 Type: Default 1000ms 256MiB

Word Formation Challenge

Word Formation Challenge

You are given a target word and a dictionary of words. Your task is to determine whether the target word can be formed by concatenating one or more words from the dictionary. A word in the dictionary may be used multiple times if necessary. For example, if the dictionary contains 'apple' and 'pie', the target word 'applepie' can be formed, so the answer is YES. Otherwise, the answer is NO.

Note: The solution must process multiple test cases from standard input and output the results to standard output. Use an efficient approach such as dynamic programming to solve the problem.

inputFormat

The input begins with an integer T on the first line, representing the number of test cases. Each test case consists of three lines:

  1. A string denoting the target word.
  2. An integer N representing the number of words in the dictionary.
  3. A line with N space-separated words that form the dictionary.

outputFormat

For each test case, output a single line containing either YES if the target word can be formed by concatenating words from the dictionary, or NO otherwise.## sample

1
applepie
3
apple pie plap
YES

</p>