aboutsummaryrefslogtreecommitdiff
path: root/codechef/DenseBracketSequence/main.cpp
blob: 6785a1241f25910c22d613b4d4f1ccfb1f5c06a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int count = 0;
    stack<char> st;
    st.push(s[0]);
    for(int i = 1; i < n; i++) {
      if(s[i] == '(') {
        st.push('(');
      }
      if(s[i] == ')') {
        if(st.top() == '(' && !st.empty()){
          st.pop();
          count++;
        }
      }
    }
    cout << count << endl;
  }
}