#46133: 蛤就..過了


s214094@sphs.hc.edu.tw (機一孝46鄭家宇)

學校 : 私立磐石中學
編號 : 256026
來源 : [1.34.231.158]
最後登入時間 :
2025-05-27 17:46:40

想法是left紀錄'('出現次數
..然後right沒有用
當遍歷到')'就把左邊括號(left)-- 然後成對的total++

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string c;
int main()
{
    int n=0;
    cin>>n;
    for(int i=0;i<n;i++){
        int left=0,right=0,total=0;
        cin>>c;
        for(int j=0;j<c.length();j++){
            if(c[j]=='('){
                left++;
               }
            if(c[j]==')'){
                if(left==0){
                    cout<<0<<endl;
                    break;
                }
                left--;
                total++;

               }
               if(j==c.length()-1&&left==0){
                    cout<<total<<endl;
               }else if(j==c.length()-1&&left>0){
                    cout<<0<<endl;
               }

        }
    }
    return 0;
}