Given a book text and a picture of painted cells, find the shortest prefix-contiguous text segment whose pen strokes draw exactly that picture up to translation.
Medium7String matchingHash mapSimulationNo attempts yetTime limit2sMemory limit256 MBEugene is reading a boring book. To keep himself entertained he draws a picture while he reads. He has a sheet of graph paper divided into square cells, and every cell is empty at the start.
Eugene first paints one cell. Then he opens the book at some page and starts reading. Every time he sees the letter u he moves his pen one cell up and paints the cell under the pen. For the letter d he does the same but moves the pen one cell down. For the letters l and r he moves the pen one cell left and one cell right. Every other letter, and every space, comma and period, does nothing. He may paint a cell that is already painted.
You have the sheet of paper and the text of the book. Decide whether Eugene could have drawn this picture at some point while reading. The part he read may be any contiguous piece of the text. You do not know which cell he painted first, so the picture counts as drawable when the set of painted cells matches the given picture exactly up to translation.
The first line contains an integer l, the length of the text (1≤l≤100000).
The second line contains the text, a string of length l. It contains only lowercase English letters, spaces, commas and periods, and it neither begins nor ends with a space.
The third line contains two positive integers n and m, the size of the picture (2≤n×m≤100000).
Each of the next n lines contains a string of length m. A painted cell is X and an empty cell is .. The picture has at least two painted cells.
The first of those n lines is the top of the picture and the last one is the bottom.
If Eugene could have drawn the picture, print YES on the first line. On the second line print two integers b and e (1≤b≤e≤l) such that reading the text from its bth character to its eth character, inclusive, paints exactly the given picture up to translation. If several pairs (b,e) satisfy this, print the one with the smallest b, and among those the one with the smallest e.
If the picture cannot be drawn, print NO.