#P12238. Prefix Partitioning in LQ Country
Prefix Partitioning in LQ Country
Prefix Partitioning in LQ Country
In the distant LQ country, every word is composed exclusively of the three characters , and (with ASCII codes , , and , respectively). A magic dictionary contains words. In order to help memorize words faster, Xiaolan decides to partition the dictionary into classes by choosing distinct non‐empty prefixes with the following requirements:
- Choose different non‐empty prefixes that occur in the dictionary (each chosen prefix must be a prefix of at least one word).
- For every word in the dictionary, there must be exactly one chosen prefix that is a prefix of that word. (In other words, the chosen prefixes form a prefix–free set that covers every word exactly once.)
- Each of the classes (i.e. the set of words having the same chosen prefix) is non–empty.
Your task is to count the number of ways to choose such a set of prefixes. Two ways are considered different if the sets of chosen prefixes differ. Since the answer can be very large, output it modulo .
Formally, given a dictionary of words (each word a string over the alphabet {l, q, b}) and an integer , count the number of sets of non–empty strings satisfying:
- For every , there exists at least one word in the dictionary with prefix .
- For every word in the dictionary, there is exactly one such that is a prefix of .
Note that we do not allow the empty string as a prefix.
inputFormat
The first line contains two integers and (, ), representing the number of words in the dictionary and the required number of classes, respectively. Each of the next lines contains a non–empty string consisting only of the characters 'l', 'q' and 'b'. It is guaranteed that the total length of all words does not exceed .
outputFormat
Output a single integer: the number of ways to choose prefixes to partition the dictionary as described, modulo .
sample
1 1
l
1
</p>