Prefix-free Queries
Time limit2sMemory limit256 MB
Given a string and queries of substring lists, count the prefix-free subsets of the substrings modulo m, for each query.
- Level
Hard9 of 10
- Topics
- String, Trie, Dynamic programming, Divide and conquer
- Solved
- No attempts yet
Problem
is the number of ways to build a prefix-free set from the multiset . A prefix-free set is a set of distinct strings in which no string is a prefix of another. The empty set is a valid prefix-free set. For example, if for every the string is not a prefix of , then .
We count the ways to build such sets, not the sets themselves. That is, we count the subsets of indices from whose strings form a prefix-free set. For example, ("aa", "aa", "a", "a") . The five ways are the empty set, and the sets containing only the first, second, third, or fourth string.
You are given a string of lowercase English letters and queries. Let be the substring . For each query " ", print modulo .
Input
The first line contains two integers and (, ). The second line contains the string of length . Each of the next lines contains one query in the form " ", where , , and . The sum of all over all queries does not exceed .
Output
For each query, print one line with the value of modulo .