Unicorn
Time limit2sMemory limit128 MB
Count the number of paths on an N by M letter grid where a chess unicorn piece spells out a given word, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Prefix sum, Matrix, Combinatorics
- Solved
- No attempts yet
Problem
A unicorn is a chess piece similar to a knight. A knight moves two squares in one direction and one square in a perpendicular direction. A unicorn instead moves more than two squares in one of the four cardinal directions, then more than one square in one of the two perpendicular directions.
More precisely, one move is performed as follows.
- Pick up the unicorn.
- Move it at least squares in one of the four cardinal directions.
- Move it at least squares in one of the two directions perpendicular to the direction just used.
- Put the unicorn down.
The chessboard has size . Each square contains one of the first uppercase English letters.
Given , , , and a word, count the number of unicorn paths whose occupied squares spell the given word in order. Print the count modulo .
Input
The first line contains , , and . Both and are positive integers no greater than , and is a positive integer no greater than .
The second line contains the word. Its length is at most , and it consists only of uppercase English letters.
From the third line, lines follow. Each of these lines is a string of length describing the letters written on the chessboard.
Output
Print the number of paths modulo on the first line.