#P6999. [NEERC 2013] Dictionary

[NEERC 2013] Dictionary

题目描述

Petr and Dmitry are working on a novel data compression scheme. Their task is to compress a given set of words. To compress a given set of words they have to build a rooted tree. Each edge of the tree is marked with exactly one letter.

Let us define a dictionary that is produced by this kind of tree as a set of words that can be constructed by concatenating letters on edges on any path from any vertex in the tree (not necessarily root) and going away from root down to the leaves (but not necessarily finishing on a leaf).

Boys have to construct such a tree with a dictionary that is a superset of the set of words that they are given to compress. This tree should have the smallest number of vertices between trees that satisfy the above condition. Any tree with the same number of vertices will do. Your task is to help them.

For example, in a tree on the picture above with the root marked as 11 , a path from 77 to 55 reads north, a path from 1616 to 1212 reads eastern, a path from 2929 to 22 reads european, a path from 33 to 2525 reads regional, and a path from 11 to 3131 reads contest.

输入格式

The first line of the input file contains the number of words in a given set n(1n50)n (1 \le n \le 50) . The following nn lines contain different non-empty words, one word per line, consisting of lowercase English letters. The length of each word is at most 1010 characters.

输出格式

On the first line output the number of vertices in the tree mm . The following mm lines shall contain descriptions of tree vertices, one description per line. Vertices are indexed from 11 to nn in the order of their corresponding description lines. If the corresponding vertex is a tree root, then its description line shall contain a single integer number 00 , otherwise its description line shall contain an index of its parent node and a letter on the edge to its parent node, separated by a space.

5
north
eastern
european
regional
contest

31
0
7 n
2 o
18 t
4 h
29 e
17 a
7 s
8 t
9 e
10 r
11 n
6 u
13 r
14 o
15 p
16 e
3 r
18 e
19 g
20 i
21 o
22 n
23 a
24 l
1 c
26 o
27 n
28 t
6 s
30 t

提示

Time limit: 1 s, Memory limit: 128 MB.