ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1183. Brackets Sequence

yet one more dp solution explanation
Posted by imaginary friend 16 Oct 2018 03:50
as other participants i used dp. there are different explanations of dp approach, but somehow i find them pretty blurry. when calculating the min number of brackets to be added for the substring, i think there should be done only 2 simple steps:

1) check whether the substring is already correct - can be done using stack in linear time. if this happens to be true, than no sense going to step 2
2) if s is substring and d[i][j] is used for storing dynamic for substring of i length starting at j, then solution is:
d[i][j] = min(d[i - 2][j + 1] if s[j] == s[i + j], min(d[k][j] + d[i - k][j + k]) for 1 <= k < i))

why the 2nd step has that condition? that goes from the the problem definition itself. we can form regular sequence either from wrapping it in brackets, eg '(  (....)  )' or with concatenating two regular sequences, eg '( ...)    [ ... ]'.

hope this explanation brings some clarity on the approach.