eolymp
bolt
Try our new interface for solving problems
Problems

Cellphone Typing

Cellphone Typing

A research team is developing a new technology to save time when typing text messages in mobile devices. They are working on a new model that has a complete keyboard, so users can type any single letter by pressing the corresponding key. In this way, a user needs \textbf{P} keystrokes to type a word of length \textbf{P}. However, this is not fast enough. The team is going to put together a dictionary of the common words that a user may type. The goal is to reduce the average number of keystrokes needed to type words that are in the dictionary. During the typing of a word, whenever the following letter is uniquely determined, the cellphone system will input it automatically, without the need for a keystroke. To be more precise, the behavior of the cellphone system will be determined by the following rules: \begin{enumerate} \item The system never guesses the first letter of a word, so the first letter always has to be input manually by pressing the corresponding key. \item If a non-empty succession of letters \textbf{c_1c_2...c_n} has been input, and there is a letter \textbf{c} such that every word in the dictionary which starts with \textbf{c_1c_2...c_n} also starts with \textbf{c_1c_2...c_nc}, then the system inputs \textbf{c} automatically, without the need of a keystroke. Otherwise, the system waits for the user. \end{enumerate} For instance, if the dictionary is composed of the words '\textbf{hello}', '\textbf{hell}', '\textbf{heaven}' and '\textbf{goodbye}', and the user presses '\textbf{h}', the system will input '\textbf{e}' automatically, because every word which starts with '\textbf{h}' also starts with '\textbf{he}'. However, since there are words that start with '\textbf{hel}' and with '\textbf{hea}', the system now needs to wait for the user. If the user then presses '\textbf{l}', obtaining the partial word '\textbf{hel}', the system will input a second '\textbf{l}' automatically. When it has '\textbf{hell}' as input, the system cannot guess, because it is possible that the word is over, or it is also possible that the user may want to press '\textbf{o}' to get '\textbf{hello}'. In this fashion, to type the word '\textbf{hello}' the user needs three keystrokes, '\textbf{hell}' requires two, and '\textbf{heaven}' also requires two, because when the current input is '\textbf{hea}' the system can automatically input the remainder of the word by repeatedly applying the second rule. Similarly, the word '\textbf{goodbye}' needs just one keystroke, because after pressing the initial '\textbf{g}' the system will automatically fill in the entire word. In this example, the average number of keystrokes needed to type a word in the dictionary is then \textbf{(3 + 2 + 2 + 1)/4 = 2.00}. Your task is, given a dictionary, to calculate the average number of keystrokes needed to type a word in the dictionary with the new cellphone system. \InputFile Each test case is described using several lines. The first line contains an integer N representing the number of words in the dictionary (\textbf{1} ≤ \textbf{N} ≤ \textbf{10^5}). Each of the next \textbf{N} lines contains a non-empty string of at most \textbf{80} lowercase letters from the English alphabet, representing a word in the dictionary. Within each test case all words are different, and the sum of the lengths of all words is at most \textbf{10^6}. \OutputFile For each test case output a line with a rational number representing the average number of keystrokes needed to type a word in the dictionary. The result must be output as a rational number with exactly two digits after the decimal point, rounded if necessary.
Time limit 5 seconds
Memory limit 64 MiB
Input example #1
4
hello
hell
heaven
goodbye
3
hi
he
h
7
structure
structures
ride
riders
stress
solstice
ridiculous
Output example #1
2.00
1.67
2.71
Source ACM ICPC Regional Latino America 2012