Concert Attendance Schedules
Time limit0.3sMemory limit128 MB
Count the ways to pick increasing day positions matching a target band sequence, where each pick must wait h_b+1 days after that band's previous pick.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
John listens to 26 bands, labeled A through Z. He checked the schedule for the coming season and found that exactly one concert is held on each of the next days. He wants to attend exactly of those concerts in a fixed order, and he may attend concerts of the same band more than once.
Some bands charge more than others, so after attending a concert by band John rests at home for at least days before he attends another concert. If he attends a concert of band on day , the next concert he attends is on day or later.
Count the schedules that let John attend the concerts in the order he wants. The count can be very large, so print it modulo .
Input
The first line contains and , separated by a space (, ).
The second line contains the 26 values through , separated by spaces ().
The third line contains a string of length listing the bands whose concerts John wants to attend, in order. For example, AFJAZ means A first, then F, then J, then A, then Z.
The fourth line contains a string of length written the same way, giving the band that performs on each of the next days.
Both strings consist of uppercase letters only.
Output
Print the number of schedules that let John attend the concerts in the order he wants, modulo , on one line.