【題目敘述】https://zerojudge.tw/ShowProblem?problemid=b838
【解題想法】利用stack先進後出的特性,處理括號配對。

#include <iostream>
#include <stack>
using namespace std;
int main() {
int n;
string s;
cin >> n;
while (n--) {
int count = 0;
stack <int> stk;
cin >> s;
for (int j = 0; j < s.length(); j ++){
if (!stk.empty() && stk.top() == '(' && s[j] == ')'){
stk.pop();
count ++;
}else{
stk.push(s[j]);
}
}
if (!stk.empty()){
count = 0;
}
cout << count << endl;
}
}