Trie Sharding
Time limit5sMemory limit512 MB
Split the strings among N labeled non-empty servers to maximize the total trie node count and report the maximum and the count of optimal splits modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Trie, Combinatorics
- Solved
- No attempts yet
Problem
A set of strings can be stored compactly in a trie. A trie is a rooted tree that has exactly one node for every distinct prefix of the strings in , counting the empty prefix.
For example, if is "AAA", "AAB", "AB", "B", the trie has 7 nodes, one for each of the prefixes "", "A", "AA", "AAA", "AAB", "AB", "B".
One server holds all of in a single trie. has grown too large to fit in that server's memory, so is spread over servers instead. is split into disjoint non-empty subsets , and server builds a trie that holds only the strings in . The total number of nodes over all tries can then be larger than the number of nodes in the single trie, and the way the strings are split cannot be controlled.
Take the same four strings and split them over two servers, "AAA" and "B" on the first server, "AAB" and "AB" on the second. The first trie needs 5 nodes ("", "A", "AA", "AAA", "B") and the second trie also needs 5 nodes ("", "A", "AA", "AAB", "AB"). The two servers hold 10 nodes together, against the 7 nodes one server needs for all four strings.
Given and , find the largest total number of nodes that a split can produce, and count the splits that reach it. The servers are distinguishable: if a string lies in in one split and in with in another split, the two splits are different. Report the count modulo 1,000,000,007.
Input
The first line has the number of test cases . Each test case starts with a line holding two integers and separated by a space. The next lines each hold one string of .
Limits
- Every string of has 1 to 100 characters and uses upper case English letters only.
- The strings of are all distinct.
Output
For each test case print one line in the form "Case #i: X Y", where is the test case number starting from 1, is the largest total number of nodes over all tries, and is the number of splits whose total node count is , taken modulo 1,000,000,007.