#P13259. [GCJ 2014 #2] Trie Sharding

    ID: 15125 远端评测题 3000~5000ms 1024MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2014树形 DP组合数学字典树 TrieGoogle Code Jam

[GCJ 2014 #2] Trie Sharding

题目描述

A set of strings S\mathbf{S} can be stored efficiently in a trie. A trie is a rooted tree that has one node for every prefix of every string in S\mathbf{S}, without duplicates.

For example, if S\mathbf{S} were "AAA", "AAB", "AB", "B", the corresponding trie would contain 7 nodes corresponding to the prefixes "", "A", "AA", AAA", "AAB", "AB", and "B".

I have a server that contains S\mathbf{S} in one big trie. Unfortunately, S\mathbf{S} has become very large, and I am having trouble fitting everything in memory on one server. To solve this problem, I want to switch to storing S\mathbf{S} across N\mathbf{N} separate servers. Specifically, S\mathbf{S} will be divided up into disjoint, non-empty subsets $\mathbf{T}_{1}, \mathbf{T}_{2}, \ldots, \mathbf{T}_{\mathbf{N}}$, and on each server i, I will build a trie containing just the strings in Ti\mathbf{T}_{\mathbf{i}}. The downside of this approach is the total number of nodes across all N\mathbf{N} tries may go up. To make things worse, I can't control how the set of strings is divided up!

For example, suppose "AAA", "AAB", "AB", "B" are split into two servers, one containing "AAA" and "B", and the other containing "AAB", "AB". Then the trie on the first server would need 5 nodes ("", "A", "AA", "AAA", "B"), and the trie on the second server would also need 5 nodes ("", "A", "AA", "AAB", "AB"). In this case, I will need 10 nodes altogether across the two servers, as opposed to the 7 nodes I would need if I could put everything on just one server.

Given an assignment of strings to N\mathbf{N} servers, I want to compute the worst-case total number of nodes across all servers, and how likely it is to happen. I can then decide if my plan is good or too risky.

Given S\mathbf{S} and N\mathbf{N}, what is the largest number of nodes that I might end up with? Additionally, how many ways are there of choosing $\mathbf{T}_{1}, \mathbf{T}_{2}, \ldots, \mathbf{T}_{\mathbf{N}}$ for which the number of nodes is maximum? Note that the N\mathbf{N} servers are different -- if a string appears in Ti\mathbf{T}_{\mathbf{i}} in one arrangement and in Tj\mathbf{T}_{\mathbf{j}} (iji \neq j) in another arrangement, then the two arrangements are considered different. Print the remainder of the number of possible arrangements after division by 1,000,000,007.

输入格式

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case starts with a line containing two space-separated integers: M\mathbf{M} and N\mathbf{N}. M\mathbf{M} lines follow, each containing one string in S\mathbf{S}.

输出格式

For each test case, output one line containing "Case #i: XY\mathbf{X} \mathbf{Y}", where i is the case number (starting from 1), X\mathbf{X} is the worst-case number of nodes in all the tries combined, and Y\mathbf{Y} is the number of ways (modulo 1,000,000,007) to assign strings to servers such that the number of nodes in all N\mathbf{N} servers are X\mathbf{X}.

2
4 2
AAA
AAB
AB
B
5 2
A
B
C
D
E
Case #1: 10 8
Case #2: 7 30

提示

Limits

  • 1T1001 \leq T \leq 100.
  • Strings in S\mathbf{S} will contain only upper case English letters.
  • The strings in S\mathbf{S} will all be distinct.
  • NM\mathrm{N} \leq \mathrm{M}

Small dataset(9 Pts)

  • Time limit: 60 3 seconds.
  • 1M81 \leq M \leq 8
  • 1N41 \leq N \leq 4
  • Each string in S\mathbf{S} will have between 1 and 10 characters, inclusive.

Large dataset(30 Pts)

  • Time limit: 120 5 seconds.
  • 1M10001 \leq M \leq 1000
  • 1N1001 \leq N \leq 100
  • Each string in S\mathbf{S} will have between 1 and 100 characters, inclusive.