Problem 655
Strange Printer
Hard · 30 pts · String, Dynamic Programming
Problem
A strange printer can only print a run of one repeated character per turn, and each turn may start and end anywhere, covering whatever was there before. Find the minimum number of turns needed to print a given string. Let turns(i, j) be the answer for S[i..j]. Printing S[j] on its own costs turns(i, j - 1) + 1, but if some S[k] with i <= k < j equals S[j], S[j] can be printed in the same turn as S[k], giving turns(i, k) + turns(k + 1, j - 1).
Input
A single string S of lowercase English letters.
Output
Print the minimum number of turns.
Constraints
1 <= |S| <= 100
Example input
aba
Example output
2
Explanation
For aba, first print aaa across the whole string, then print b over the middle character. That takes 2 turns, and one turn cannot produce two different letters.