Find String in a Grid
Time limit5sMemory limit512 MB
For each query string, count the right-then-down paths in a 500 by 500 grid that spell it out, with up to 200000 queries.
- Level
Medium7 of 10
- Topics
- String matching, Trie, Hash map
- Solved
- No attempts yet
Problem
You have a grid with rows (numbered from 1 to , top to bottom) and columns (numbered from 1 to , left to right). Each cell holds one uppercase character. The character in row and column is . You also have strings made of uppercase characters. For each string, count the number of occurrences of the string in the grid.
An occurrence of string is counted when can be built by starting at a cell, moving right 0 or more times, and then moving down 0 or more times. Two occurrences are different if the sets of cells used to build the string are different. Formally, for each string , count the tuples that satisfy:
and
Input
The first line contains three integers , , and (, ). The next lines each contain uppercase characters. The -th character on the -th line is . The next lines each contain one string of uppercase characters. Each string has length at least 1 and at most 200 000, and the total length of all strings is at most 200 000.
Output
For each query, in input order, print on its own line the number of occurrences of the string in the grid.