Type Printer
Time limit1sMemory limit128 MB
Find the minimum number of add, remove, and print operations to type N distinct words on a printer that keeps a single editable string, with any print order allowed.
Problem
You need to print words on a movable type printer. A movable type printer is one of those old printers that require you to place small metal pieces (each engraved with a single letter) in order to form a word, after which a sheet of paper is pressed against them to print the word. The printer you have supports the following three operations:
- Add a single letter to the end of the word currently in the printer.
- Remove the last letter from the word currently in the printer. This is allowed only if there is at least one letter in the printer.
- Print the word currently in the printer.
Initially the printer is empty; it contains no letter pieces. After all printing is done, you are allowed to leave some letters in the printer. You may also print the words in any order you like.
Every operation takes time, so you want to minimize the total number of operations.
Given the words you want to print, write a program that finds the minimum number of operations needed to print all the words in some order.
Input
The first line contains the integer , the number of words to print. ()
Each of the next lines contains one word. Each word consists only of lowercase letters ('a'–'z') and has length between and , inclusive. All words are distinct.
Output
Print a single integer : the minimum number of operations required to print all the words.