Given a bracket sequence or in other words a string S of length n, consisting of characters ‘(‘ and ‘)’. Find length of the maximum correct bracket subsequence of sequence for a given query range. Note: A correct bracket sequence is the one that have matched bracket pairs or which contains another nested correct bracket sequence. For e.g (), (()), ()() are some correct bracket sequence.
Input : S = ())(())(())( Start Index of Range = 0, End Index of Range = 11 Output : 10 Explanation: Longest Correct Bracket Subsequence is ()(())(()) Input : S = ())(())(())( Start Index of Range = 1, End Index of Range = 2 Output : 0
Segment Trees can be used to solve this problem efficiently
At each node of the segment tree, we store the following:
1) a - Number of correctly matched pairs of brackets. 2) b - Number of unused open brackets. 3) c - Number of unused closed brackets.
(unused open bracket – means they can’t be matched with any closing bracket, unused closed bracket – means they can’t be matched with any opening bracket, for e.g S = )( contains an unused open and an unused closed bracket)
For each interval [L, R], we can match X number of unused open brackets ‘(‘ in interval [L, MID] with unused closed brackets ‘)’ in interval [MID + 1, R] where
X = minimum(number of unused ‘(‘ in [L, MID], number of unused ‘)’ in [MID + 1, R])
Hence, X is also the number of correctly matched pairs built by combination.
So, for interval [L, R]
1) Total number of correctly matched pairs becomes the sum of correctly matched pairs in left child and correctly matched pairs in right child and number of combinations of unused ‘(‘ and unused ‘)’ from left and right child respectively.
a[L, R] = a[L, MID] + a[MID + 1, R] + X
2) Total number of unused open brackets becomes the sum of unused open brackets in left child and unused open brackets in right child minus X (minus – because we used X unused ‘(‘ from left child to match with unused ‘) from right child).
a[L, R] = b[L, MID] + b[MID + 1, R] - X
3) Similarly, for ununsed closed brackets, following relation holds.
a[L, R] = c[L, MID] + c[MID + 1, R] - X
where a, b and c are the representations described above for each node to be stored in.
Below is the implementation of above approach in C++.
Maximum Length Correct Bracket Subsequence between 0 and 11 = 10 Maximum Length Correct Bracket Subsequence between 1 and 2 = 0
Time complexity for each query is O(logN), where N is the size of string.
- Range Queries for Longest Correct Bracket Subsequence Set | 2
- Number of balanced bracket subsequence of length 2 and 4
- Array range queries over range queries
- Binary Indexed Tree : Range Update and Range Queries
- Find index of closing bracket for a given opening bracket in an expression
- Queries on subsequence of string
- Subsequence queries after removing substrings
- Range LCM Queries
- Min-Max Range Queries in Array
- Range sum queries without updates
- Two equal sum segment range queries
- Queries on XOR of greatest odd divisor of the range
- Range product queries in an array
- Range and Update Sum Queries with Factorial
- Longest Uncommon Subsequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.