3

Consider B to be a sequence of grouping symbols (, ), [, ], {, and }. B is called a Balanced sequence if it is of length 0 or B is of one of the following forms: { X } Y or [ X ] Y or { X } Y where X and Y are Balanced themselves. Example for Balanced : ( ) - { [ ] ( ) } [ ] - ...

Now the question is to find an efficient algorithm to find maximum length balanced subsequence (not necessarily contiguous) of a given input which is an string of these grouping symbols.

For example if input is ( ) { ( [ ) ] { ( ] ) } } [ ] , one of the max-length subsequences is ( ) { [ ] { ( ) } } [ ]

I am almost sure the solution is DP but anyway I solve it I find examples in which my algorithm doesn't work. there is only one approach I am sure about, which is a combination of DP and Divide and Conquer. But it is not efficient because anyway D&C part is going to solve some overlapping problems over and over again.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Nima
  • 276
  • 3
  • 15

1 Answers1

5

Let's make a couple of simple observation:

  1. Any balanced subsequence can be represented as A1 X A2 Y, where A1 and A2 are two matched brackets((), [] or {}), X and Y are balanced subsequences(they can be empty). It is true because there is a leftmost bracket in any balanced non-empty subsequence and it must be matched to something.

  2. X and Y are independent. If it is not the case, the subsequence is not balanced.

These observations give us a dynamic programming solution:

Let's assume f(L, R) is the longest balanced subsequence for [L, R] subarray. The base is an empty subarray. Transition are as follows:

f(L, R) = max(   
     f(L + 1, R) // ignore the first element    
     max(f(L + 1, K - 1) + 2 + f(K + 1, R)) for all K if brackets at positions L and K match
)

The time complexity is O(N^3) because there are O(N^2) subarrays and there are O(N) transitions for each of them. It is possible to reconstruct the longest subsequence itself using standard answer reconstruction techniques.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • So as I get it range for k must be L+2 <= k <= R. But, what if L matches L+1 and it is part of answer? – Nima Dec 20 '14 at 23:07
  • 1
    @Nima I assume that we can use an empty range with L > R(the answer is zero for it). It allows us to avoid handling this case specially. – kraskevich Dec 20 '14 at 23:30
  • The Knuth-Yao quadrangle inequality speedup applies to this problem, right? So you can do it in O(n^2)? – harold Dec 29 '14 at 22:25
  • 1
    @harold No, it does not apply to this problem. For example, it is not true for a string ({)} and a = 0, b = 1, c = 2 and d = 3. – kraskevich Dec 29 '14 at 22:34