String Folding
InterviewTime limit2sMemory limit128 MB
Find the length of the shortest folded sequence, using repeat counts like 3(AB), that unfolds to the given uppercase string.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String
- Solved
- No attempts yet
Problem
Bill wants to compactly represent strings of uppercase letters 'A' to 'Z' by folding their repeating parts. For example, the string AAAAAAAAAABABABCCD can be written as 10(A)2(BA)B2(C)D.
A folded sequence and its unfolding are defined as follows.
- A string consisting of a single character from 'A' to 'Z' is a folded sequence. Unfolding it yields that same single character.
- If and are folded sequences, then is also a folded sequence. If unfolds to and unfolds to , then unfolds to .
- If is a folded sequence, then is also a folded sequence, where is the decimal representation of an integer greater than 1. If unfolds to , then unfolds to repeated times.
Among all folded sequences that unfold to the given string, find the number of characters in the shortest one.
Input
A single line containing a string of uppercase letters from 'A' to 'Z'. Its length is between 1 and 100, inclusive.
Output
Print a single integer: the number of characters in the shortest folded sequence that unfolds to the input string.