String Path
Time limit1sMemory limit128 MB
Count the N by M letter grids where two given strings each appear on a down-right path from the top-left to the bottom-right corner.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics
- Solved
- No attempts yet
Problem
There is a rectangular grid of size . Every cell holds one uppercase letter. Rows are numbered 1 to from top to bottom, and columns are numbered 1 to from left to right.
Consider a route that starts at , moves one cell down or one cell right at each step, and ends at . Writing every letter of the visited cells in the order they are visited gives a string of length , and that string is called a string path of the grid. Different routes can give several string paths in one grid.
You are given two different strings of length . Write a program that counts the rectangular grids for which both strings are string paths. Two grids count as different when some position holds a different letter. The count can be large, so print it modulo 1,000,000,009.
Input
The first line contains two natural numbers and . ()
The second line and the third line each contain one string of uppercase letters of length . The two strings are different.
Output
Print the number of rectangular grids for which both given strings are string paths, modulo 1,000,000,009.
Hint
With , and the strings ABC and ADC, the two grids below satisfy the condition.