Brackets all cover Dynamic Programing

this is the editorial for the problem
This problem can be solved with dynamic programming:

  1. Calculate dp[i, j] : How many sequences of brackets of length i has balance j and intermediate balance never goes below zero (They form a prefix of a valid sequence of brackets).

  2. For the given sequence of length n calculate the resulting balance a and the minimum balance b.

  3. Try the length of the sequence added at the beginning c and its balance d. If - b ≀ d then add dp[c, d] Γ— dp[m - n - c, d + a] to the answer.

i cannot understand how to calculate dp[i][j] efficiently
i know that if a string has balanced parantheses then for length n the value will be 2nCn/(n+1)
but how to calculate for a particular balance factor??
also, how can we store so many values…m,n go upto 10^5 and each length n can have balance 0,1,2,…upto n

@shameek.agarwal
Let dp[i][j] = number of bracket sequences of length i and balance j.
Let’s formulate a recurrence for dp[i][j]. To do that, consider a bracket sequence of length i and balance j.
There are two cases: It’s either the sequence ends with a β€˜)’ or it ends with a β€˜(’. Let’s consider these cases.

Case 1: Ends with β€˜)’
Then if we remove that last bracket, the remaining sequence must have a balance of j + 1, because that last β€˜)’ must have matched a β€˜(’ and thus reduced the number of unmatched left brackets to j.
So we have dp[i][j] = dp[i β€” 1][j + 1] in this case.

Case 2: Ends with β€˜(’
Then if we remove that last bracket, the remaining sequence must have a balance of j β€” 1, because that last β€˜(’ matches nothing and only adds to the number of unmatched left brackets.
In this case, we have dp[i][j] = dp[i β€” 1][j β€” 1]

Since case 1 and case 2 are just partitions of dp[i][j], we combine them to get the total possibilities for dp[i][j]:
dp[i][j] = dp[i β€” 1][j + 1] + dp[i β€” 1][j β€” 1]

You can think of it as if you are given a sequence of brackets, first you should check the validity of these brackets eg: ())) - This is an incomplete sequence as number of opening and closing brackets are not same. And eg: ()() - This is a complete sequence. So now you have to make two different sequences which should balance the overall string a+s+b. So a can be independent or dependent on s or b and same applies to b. So you can make a dp to store the number by which the number of opening brackets are greater than the number of closing brackets till that index i. And then as stated in editorial you can add product of minimum balance dp and remaining balance dp to the answer.
Hope it Helps.

THANKS FOR THE EXPLANATION!!
mycode here
i am getting wrong answer for a case like
6 2 ((
anwer should be 3
i am getting 5
i have also printed debug statements…can u please help??

@shameek.agarwal hey your code is correct .for 6 2 ((
you can get 5 cases
’ ’ + (( + β€˜))()’
’ ’ + (( + β€˜)())’
’ ’ + (( + β€˜()))’
'( ’ + (( + β€˜)))’
β€˜()’ + (( + β€˜))’

@aa1 can you please point out my mistake then?? i tried downloading the testcases but how can i make out something from strings of 1000 length??
thanksss

@shameek.agarwal refer to this code

1 Like