Mate
Time limit2sMemory limit128 MB
For each query, count subsequences of S of length D whose last two characters are the given pair XY, modulo 1e9+7.
- Level
Medium7 of 10
- Topics
- Combinatorics, Dynamic programming, Prefix sum, Math
- Solved
- No attempts yet
Problem
Mate got a string of lowercase English letters from his parents. To get at least some use out of the present, he decided to find rhymes in it for the song he is writing next.
A rhyme is a word of length whose next to last letter is and whose last letter is . Mate builds a word by crossing out some letters of the given string and joining the letters he did not cross out in their original order. For each rhyme, count how many ways there are to cross out letters so that the remaining word meets the condition.
Two ways count as different when the sets of crossed-out positions differ.
Input
The first line contains a string of lowercase English letters. ()
The second line contains the integer , the number of rhymes Mate needs. ()
Each of the next lines contains an integer and a string of two lowercase English letters, separated by a space. ()
Output
Print lines. The th line contains the number of ways for the th rhyme. The number can get very large, so print it modulo .
Note
Take = banana with and = na. Number the letters from 1. Exactly three sets of kept positions work: , and . In each of them the kept letters spell na, and every other letter is crossed out.