洛谷 - P1739 - 表达式括号匹配 - 模拟 - 栈

https://www.luogu.org/problemnew/show/P1739

虽然应该是用栈的……但是直接模拟就可以了。

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

char s[300];
int main(){
    fgets(s,300,stdin);
    int n=strlen(s);
    int cntl=0;
    for(int i=0;i<n;i++){
        if(s[i]=='(')
            cntl++;
        else if(s[i]==')'){
            if(cntl==0){
                printf("NO\n");
                return 0;
            }
            cntl--;
        }
    }

    if(cntl){
        printf("NO\n");
        return 0;
    }
    else{
        printf("YES\n");
        return 0;
    }
}

 

posted @ 2019-01-25 05:30  韵意  阅读(168)  评论(0编辑  收藏  举报