AutoComplete Tool
Time Limit: 2 Seconds Memory Limit: 65536 KB
As ACM programming contest in Tehran site is getting closer and closer, MIT team is training more and more to finally get qualified for the final contest. All three members of MIT team are now expert in designing and implementing algorithms even complicated ones but they suffer from slow typing. Although they have been typing for many years, their typing speed is the same as that of a beginner programmer. This problem, together with a limited time in an ACM programming contest, has been resulted in their obtaining a world record of getting no balloons while attending the most ACM contests.
To reach the dream of attending the final contest, they have decided to develop a software, named AutoComplete Tool (ACT for short), to expedite their coding. ACT aims to reduce the number of key-pressings needed to type a code (or a text) by predicting words. For a text to be typed, assume that ACT knows all words in the text. ACT constructs a mapping table which maps a string s to a word (denoted by w(s)) of the text where s is a non-empty prefix of w(s).
ACT uses the mapping table to suggest w(s) to a user when ss' is being typed by the user. ACT suggestions are consistent, i.e., if the suggested word of s is w(s) and ss' (the concatenation of two strings s and s') is a prefix of w(s), then w(ss') = w(s). When a user starts typing the text, ACT helps the user as follows. Upon typing a letter, ACT suggests w(s) where s is the string of letters being typed so far. Pressing "enter" key results in displaying the suggested word without typing any more letters. In the case of pressing "space" key (" "), the word being typed so far is displayed. In both cases, a space is displayed after the displayed word. For instance, assume the text is "hello heat heats ". In the mapping table, suppose ACT maps "h" and "hel" to "heats" and "hello" respectively. Pressing the keys "h", "e", "l", "(enter)", "H", "e", "a", "t", " ", "h", "(enter)" in the given order displays the text. Note that the suggested word of "h", "he", "hea", "heat" and "heats" is "heats". Assuming ACT constructs the mapping table in such a way that the number of key-pressings is minimum, your task is to compute the minimum number of key-pressings using ACT for typing a text.
Input
The input consists of several test cases. Each test case begins with an integer n (0 < n < 10000) denoting the number of words in the text. Each of the next n lines contains a word. Words are strings of lowercase letters (a..z) of size at least 1 and at most 15. The input terminates with a line containing 0.
Output
For each test case, output a single line containing the minimum number of key-pressings needed to type all words of the text when an ACT is available. Note in the text, words are separated by a space and there is a space after the last word.
Sample Input
1 hello 3 heat hello heats 2 da dc 2 abc abc 0
Sample Output
2 11 5 4Submit
Source: Tehran, Asia Region - Regional 2011