【Luogu1739】表达式括号匹配

problem

solution

codes

#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main(){
    stack<char>s;
    string str;  cin>>str;
    bool flag = 1;
    for(int i = 0; i < str.size(); i++){
        if(str[i] == '(')s.push('(');
        else if(str[i] == ')' && !s.empty())s.pop();
        else if(str[i] == ')' && s.empty()){ flag = 0; break; } 
    }
    if(s.empty() && flag)cout<<"YES\n";//md答案写反了
    else cout<<"NO\n";
    return 0;
}
posted @ 2018-06-08 22:08  gwj1139177410  阅读(120)  评论(0)    收藏  举报
选择