20分求调

B3758 [信息与未来 2021] 括号序列

liyilin2011 @ 2024-05-06 11:34:13

#include <bits/stdc++.h>
using namespace std;

int main(){
    int cnt_l=0,cnt_r=0;
    string s;
    cin>>s;
    //cout<<s.size();
    for (int i=0;i<s.size();i++){
        if (s[i]=='(') cnt_l++;
        else cnt_r++;
    }
    //cout<<cnt_l<<" "<<cnt_r<<endl;
    cout<<s;
    for (int i=1;i<=abs(cnt_l-cnt_r);i++){
        if (cnt_l>cnt_r) cout<<")";
        else cout<<"(";
    }
    cout<<endl;
    return 0;
}

|