Classic Quotation
Time limit1sMemory limit512 MB
Given strings S and T and queries (L, R), compute the expected number of occurrences of T after deleting a random substring spanning positions L to R, scaled by the number of choices.
- Level
Hard8 of 10
- Topics
- String matching, Prefix sum, Combinatorics, Math
- Solved
- No attempts yet
Problem
When chatting online, we can save what somebody said to form his classic quotation. Little Q does this, too. And what's more, he can even change the original words. Formally, assume somebody said a string of length . Little Q will choose a continuous substring of (possibly empty) and remove it, then concatenate the two remaining parts, obtaining a new string . For example, he might remove "not " from the string "I am not SB", so that the new string will be "I am SB".
After doing lots of such things, Little Q finds out that string occurs as a continuous substring of very often.
Now given strings and , Little Q has queries. Each query has the following format: given and , Little Q will remove a substring so that the two remaining parts are and where the pair of integers is chosen equiprobably among all pairs where and . Your goal is to find , the expected number of occurrences of in the resulting string, and print the value .
All occurrences of must taken into account even if they overlap. The queries are independent: the string actually does not transform into and is the same for all queries.
Input
The first line of the input contains three integers , and denoting the length of , the length of and the number of queries (, , ).
The next line contains a string consisting of lowercase English letters. The following line contains a string consisting of lowercase English letters. Each of the remaining lines contains a query consisting of two integers and ().
Output
For each query, print a single line containing a single integer: the answer to the query.