fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. bool isop(char c){
  4. return c == '+' || c == '-' ||c == '*' ||c == '/';
  5. }
  6. int coduthua(string s){
  7. stack<char> st;
  8. for(char c : s){
  9. if(c == '(') st.push(c);
  10. else{
  11. if(!st.empty() && st.top()== '(') st.pop();
  12. else st.push(c);
  13. }
  14. }
  15. int cnt1 = 0, cnt2 = 0;
  16. while(!st.empty()){
  17. if(st.top() == '(') cnt1++;
  18. else cnt2++;
  19. st.pop();
  20. }
  21. if(cnt1 % 2 == 1) return ((cnt1 + cnt2) / 2) + 1;
  22. else return (cnt1 + cnt2) / 2;
  23.  
  24. }
  25. int main(){
  26. ios_base::sync_with_stdio(false);
  27. cin.tie(0);
  28. int t;
  29. cin >> t;
  30. cin.ignore();
  31. while(t--){
  32. string s;
  33. cin >> s;
  34. cout << coduthua(s);
  35. cout << "\n";
  36. }
  37.  
  38. }
Success #stdin #stdout 0.01s 5288KB
stdin
4
))((
((((
(((())
)(())(((
stdout
2
2
1
3