[ANARCO9A][1]
I am trying to solve it using following recursion ->
Let S(k, open) be the number of min operations to balance the string
with |open| open brackets by the time we start at k.
Let current bracket be cur. We have several cases:
- open is 0, and cur = ‘}’, then we have to flip.
S(k, open) = 1 + S(k+1, open+1)
open is 0 and cur = ‘{’, then we cannot flip.
S(k, open) = S(k+1, open+1) - open > 0 and cur = ‘}’. We can use cur to close an open bracket, or
flip it to add new open brackets.
S(k, open) = min( S(k+1, open-1), 1 + S(k+1, open+1) ) - open > 0 and cur = ‘{’, then we have
S(k, open) = min( S(k+1, open+1), 1 + S(k+1, open-1) )
SOURCE : [[source][2]]. but It doesn’t works for cases like -> ‘{{{}’ or ‘{{{}{{{}’
My Code ->
[3] can you help in this ?
Thanks :)
[1]: http://www.spoj.com/problems/ANARC09A/
[2]: http://abitofcs.blogspot.in/2014/10/a-bit-of-string-balancing-spoj-anarc09a.html
[3]: http://ideone.com/V5mX51