To solve the problem of finding the maximum length of a correct bracket subsequence for multiple queries efficiently, we can utilize a Segment Tree data structure. Here's a step-by-step explanation of the approach followed by the C++ implementation.
Problem Explanation
Given a string s consisting of '(' and ')' characters, we need to handle m queries where each query asks for the length of the longest correct bracket subsequence within a specified substring of s. A correct bracket sequence is one where brackets are properly matched, like "()", "(())", "()()", etc.
For each query defined by indices li and ri, we need to determine the maximum number of matched bracket pairs in the substring s[li...ri]. Since each matched pair contributes 2 to the length, the final answer for each query will be 2 * number_of_matched_pairs.
Approach Using Segment Tree
-
Segment Tree Structure:
- Each node in the segment tree will store three pieces of information:
matched: The number of matched bracket pairs in the segment.
open: The number of unmatched '(' brackets.
close: The number of unmatched ')' brackets.
-
Building the Segment Tree:
- For each single character:
- If it's
'(', it has one unmatched open bracket.
- If it's
')', it has one unmatched close bracket.
- When combining two segments:
- The number of new matched pairs is the minimum of the unmatched open brackets from the left segment and unmatched close brackets from the right segment.
- Update the
matched, open, and close accordingly.
-
Querying the Segment Tree:
- For each query
[li, ri], retrieve the corresponding segment information.
- The answer is
2 * matched, where matched is the number of matched bracket pairs in that segment.
-
Efficiency:
- Building the segment tree takes
O(n) time.
- Each query is answered in
O(log n) time.
- This approach is efficient even for large inputs (
n up to 10^6 and m up to 10^5).
C++ Implementation
Here's the C++ code implementing the above approach:
#include <bits/stdc++.h>
using namespace std;
struct Node {
int matched;
int open;
int close;
Node() : matched(0), open(0), close(0) {}
};
int n, m;
string s;
vector<Node> tree;
// Combine two nodes and return the resulting node
Node combine(const Node& left, const Node& right) {
Node res;
int matches = min(left.open, right.close);
res.matched = left.matched + right.matched + matches;
res.open = left.open + right.open - matches;
res.close = left.close + right.close - matches;
return res;
}
// Build the segment tree
void build(int node, int l, int r) {
if (l == r) {
if (s[l] == '(') {
tree[node].matched = 0;
tree[node].open = 1;
tree[node].close = 0;
}
else { // s[l] == ')'
tree[node].matched = 0;
tree[node].open = 0;
tree[node].close = 1;
}
return;
}
int mid = (l + r) / 2;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
tree[node] = combine(tree[2*node], tree[2*node+1]);
}
// Query the segment tree for range [ql, qr]
Node query_tree(int node, int l, int r, int ql, int qr) {
if (ql > r || qr < l) {
// Return an empty node
return Node();
}
if (ql <= l && r <= qr) {
return tree[node];
}
int mid = (l + r) / 2;
Node left = query_tree(2*node, l, mid, ql, qr);
Node right = query_tree(2*node+1, mid+1, r, ql, qr);
if (left.matched == 0 && left.open == 0 && left.close == 0) return right;
if (right.matched == 0 && right.open == 0 && right.close == 0) return left;
return combine(left, right);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> s;
n = s.size();
// 1-based indexing
s = " " + s;
// Allocate segment tree with size 4*n
tree.resize(4*n + 4);
build(1, 1, n);
cin >> m;
while(m--){
int li, ri;
cin >> li >> ri;
Node res = query_tree(1, 1, n, li, ri);
cout << (res.matched * 2) << "\n";
}
}
Explanation of the Code
-
Data Structures:
Node: A structure to store matched, open, and close counts.
tree: A vector representing the segment tree.
-
Functions:
combine: Merges two Node structures to form a parent node.
build: Recursively builds the segment tree.
query_tree: Recursively queries the segment tree for a given range.
-
Main Function:
- Reads the input string and appends a space at the beginning to make it 1-indexed.
- Builds the segment tree.
- Processes each query by calling
query_tree and outputs 2 * matched.
Sample Input and Output
Input:
()))(())(())
7
1 1
2 3
1 2
1 12
8 12
5 11
2 10
Output:
0
0
2
10
4
6
6
Conclusion
By leveraging a segment tree to store and combine the necessary information about bracket sequences, we can efficiently answer each query in logarithmic time, making the solution suitable for large input sizes.