Simple Prefix Compression
InterviewTime limit1sMemory limit128 MB
Sum stored lengths after prefix compression: each string stores a control byte plus the part not shared as a prefix with the previous string.
- Level
Easy3 of 10
- Topics
- String, Implementation
- Solved
- No attempts yet
Problem
Many databases store character fields (and especially indices) using prefix compression. This technique compresses a sequence of strings as follows.
The first string is stored in full. For every subsequent string , let be the length of the longest common prefix it shares with the previous string — the largest such that the first characters of and are equal. Then is stored as a single control character whose code is , followed by the remaining characters . Its stored length is therefore .
If (the two strings have no common prefix), the string is still prefixed by one control byte, so its stored length is one greater than its original length.
Compute the minimal total stored length of the whole sequence.
Input
The first line contains an integer . Each of the next lines contains one string .
Output
Print a single integer: the minimal total length of the compressed strings.