括号匹配

时间:2022-07-24
本文章向大家介绍括号匹配,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    bool flag=true;
    string s;
    cin>>n>>s;
    stack<char>st;
    for(int i=0;i<s.size();i++){
        if(s[i]=='('){
            st.push('(');
        }else{
            if(st.empty()){
                flag=false;
                break;
            }else{
                st.pop();
            }
        }
    }
    if(!st.empty())flag=false;
    if(flag)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}```