#P2856. [USACO06FEB] Cellphones G

[USACO06FEB] Cellphones G

Description

The cows have started using cellphones to cowmunicate with each other, but have found that the button layout doesn't suit their hooves very well. They are designing a special cellphone with fewer but larger buttons.

One feature they liked on the standard cellphones was predictive text. Each button has a few letters associated with it, and one types a word by pushing the associated buttons. Because there is more than one letter for each button, this can be ambiguous for some words. However, most of the time the ambiguity can be resolved by using a dictionary to determine what word the user wanted.

Since the cows are designing a custom cellphone, they are also going to replace the English alphabet with the Cow alphabet. By an amazing coincidence, the cow alphabet is simply the first L(1L26)L (1 \le L \le 26) letters of the English alphabet, in the same order. They want to find out how to assign the letters of the Cow alphabet to the BB buttons (1BL)(1 \le B \le L) in such a way that the maximum number of words in their dictionary are unambiguous when entered with predictive text. Like normal cellphones, they want the letters on each button to be a contiguous section (one or more consecutive letters) of the alphabet.

Input Format

Line 11: Two space-separated integers: BB and LL.

Line 22: DD, the number of words in the dictionary (1D1000)(1 \le D \le 1000).

Lines 3D+23 \sim D+2: Each line contains one word from the dictionary in upper case and of length 1101 \sim 10 characters. The words are presented in alphabetical order and with no duplicates.

Output Format

Line 11: The number of words in the Cow dictionary that have unique button sequences.

Lines 2B+12 \sim B+1: The nth line contains the letters that appear on button nn, in upper case and alphabetical order. The lines must be listed in alphabetical order, and every Cow letter must appear exactly once. If there is more than one optimal solution, use the one that places the most letters on button 11. Ties are broken by placing the most letters on button 22, etc.

3 13
11
ALL
BALL
BELL
CALK
CALL
CELL
DILL
FILL
FILM
ILL
MILK
7
AB
CDEFGHIJK
LM

Hint

Explanation of the sample:

Button 11 contains AB\text{AB}, button two contains CK\text{C} \sim \text{K}, and button 33 contains LM\text{LM}. The words CELL,DILL,FILL\text{CELL},\text{DILL},\text{FILL} and FILM\text{FILM} are all entered as 22332-2-3-3, while the remaining 77 words are all entered uniquely.