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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Queries for elements having values within the range A to B in the given index range using Segment Tree
- Binary Indexed Tree : Range Update and Range Queries
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Find index of closing bracket for a given opening bracket in an expression
- Queries on subsequence of string
- Subsequence queries after removing substrings
- Longest Increasing Subsequence using BIT
- Longest Common Subsequence | DP-4
- Longest Palindromic Subsequence | DP-12
- Longest dividing subsequence
- Longest Uncommon Subsequence
- Longest Subsequence of a String containing only Consonants
- Longest subsequence with different adjacent characters
- Length of Longest Balanced Subsequence
- Longest Common Anagram 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.