文章

55

粉丝

57

获赞

11

访问

10.4k

头像
括号匹配 题解:
P1501 西北工业大学2015机试题
发布于2024年3月13日 00:06
阅读数 249

#include<stdio.h>
#include<iostream>
#include<stack>
#include<string>
using namespace std;

int main(){
    stack<char> stk;
    string str;
    cin>>str;
    int len = str.length();
    for(int i = 0; i<len; i++){
        if(!stk.empty()&&(str[i] == ')' && stk.top() == '(') || (str[i] == ']' && stk.top() == '[')){
            stk.pop();
        }else{
            stk.push(str[i]);
        }
    }
    if(stk.empty()){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
}
 
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发