eolymp
bolt
Try our new interface for solving problems
Problems

Type printer

Type printer

You need to print \textbf{N} words on a movable type printer. Movable type printers are those old printers that require you to place small metal pieces (each containing a letter) in order to form words. A piece of paper is then pressed against them to print the word. The printer you have allows you to do any of the following operations: \begin{enumerate} \item Add a letter to the end of the word currently in the printer. \item Remove the last letter from the end of the word currently in the printer. You are only allowed to do this if there is at least one letter currently in the printer. \item Print the word currently in the printer. \end{enumerate} Initially, the printer is empty; it contains no metal pieces with letters. At the end of printing, you are allowed to leave some letters in the printer. Also, you are allowed to print the words in any order you like. As every operation requires time, you want to minimize the total number of operations. You must write a program that, given the \textbf{N} words you want to print, finds the minimum number of operations needed to print all the words in any order, and outputs one such sequence of operations. \textbf{1} <= \textbf{N} <= \textbf{25,000} The number of words you need to print. \textbf{Input} Your program must read from the standard input the following data: \begin{enumerate} \item Line \textbf{1} contains the integer \textbf{N}, the number of words you need to print. \item Each of the next \textbf{N} lines contains a word. Each word consists of lower case letters ('\textbf{a}' -- '\textbf{z}') only and has length between \textbf{1} and \textbf{20}, inclusive. \end{enumerate} All words will be distinct. \textbf{Output} Your program must write to the standard output the following data: \begin{enumerate} \item Line 1 must contain an integer \textbf{M} that represents the minimum number of operations required to print the \textbf{N} words. \item Each of the next \textbf{M} lines must contain one character each. These characters describe the sequence of operations done. Each operation must be described as follows: \begin{enumerate} \item Adding a letter is represented by the letter itself in lowercase \item Removing the last letter is represented by the character '\textbf{--}' (minus, \textbf{ASCII} code \textbf{45}) \item Printing the current word is represented by the character '\textbf{P}' (uppercase letter \textbf{P}) \end{enumerate} \end{enumerate}
Time limit 4 seconds
Memory limit 64 MiB
Input example #1
3
print
the
poem
Output example #1
20
t
h
e
P
-
-
-
p
o
e
m
P
-
-
-
r
i
n
t
P