洛谷 1739

· · 题解

括号匹配代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
#define maxn 10010
using namespace std;

int main(){
    stack<char> st;
    char str[maxn];
    int wrong = 0;
    gets(str);
    for (int i = 0;i < strlen(str);++i){
        if(str[i] == '(') st.push(str[i]);  
        if(str[i] == ')') {
            if(st.empty()) wrong = 1;   // 左括号少了
            else st.pop();
        }
    }
    if(wrong || !st.empty()) puts("NO");    // 左括号少了或者多了都是不匹配的
    else puts("YES");

    return 0;
}