#C10990. Find Words in Grid

    ID: 40256 Type: Default 1000ms 256MiB

Find Words in Grid

Find Words in Grid

You are given a square grid of characters of size \( n \). Each of the next \( n \) lines contains a string representing a row of the grid. Then an integer \( k \) is given followed by \( k \) words. For each word, determine whether the word can be found in the grid.

A word is considered found if it appears consecutively in any row or any column in either forward or reverse order. In other words, for a given word \( w \), check if \( w \) or its reverse \( w^R \) is a substring of any row or any column of the grid.

Note: The grid is 0-indexed. Use standard input and output for reading and writing your results.

inputFormat

The input is given via standard input (stdin) with the following format:

n
row1
row2
... 
rown
k
word1
word2
...
wordk

Where:

  • \( n \) is the size of the grid.
  • Each row is a string of exactly \( n \) characters.
  • \( k \) is the number of words to search for.
  • Each word is a string to be searched within the grid.
  • outputFormat

    For each of the \( k \) words, output a line containing either yes if the word can be found in the grid in the allowed directions, or no otherwise. The output should be printed to standard output (stdout).

    ## sample
    4
    abcd
    efgh
    ijkl
    mnop
    2
    abc
    ponm
    
    yes
    

    yes

    </p>