Topcoder SRM 555; Implementation Subtelties

This was my first non TCO div1 SRM, as last SRM advanced me to division 1. With this extra joy, came the nervousness of competing in a room where almost everyone else was supposed to be much better then me. Anyway, I had decided to focus on just the easy problem, as solving it alone would have raised my probability of gaining in rating. After struggling with it initially, I came up with the right approach to solve it. Unfortunately, I couldn’t get the implementation subtleties correct till the end, and ended up with non positive score. After the SRM, I entered practice room to validate that my DP was right, and also tried a simpler recursive method that worked as well.
DIV-1 Easy, DIV-2 Medium; CuttingBitString:

Constraints restrict the string to no more then 50 characters. So first thing I did was to generate binary representation of all powers of 5 with length no more then 50. Turns out there are only 22 such integers.

DP Solution:

A string of length 0 has 0 possible splits. For any string of length “i” greater then 0, we find solution to it by the following recurrence:

dp[i] =  min(dp[i], 1+dp[j]) for j=0 to i-1, if dp[j]>=0 and substring(j, i) is power of 5)

If solution not found, then dp[i] = -1.


A non dp recursive solution using the same array of pre generated powers of 5 is as following. Try each power of five if it is a prefix of the string, if it is, then you can you split the string into two, the prefix, and the rest of the string. Recursively do the same with the second part of the string, and your solution is 1 + (ways of splitting the rest of the string). You return the minimum of the solutions found with trying each power of 5 as the prefix.


It was an easy problem. I rushed a bit too quick into trying to implement my initial complex solution instead of trying to come up with a simpler solution. And once I did come up with a simpler solution, the time wasted on trying to get a complicated solution to work made it worse to get implementation right for the simpler one. Result; I got demoted to div2 :(.

Be Sociable, Share!

One thought on “Topcoder SRM 555; Implementation Subtelties

Leave a Reply