文章

21

粉丝

0

获赞

6

访问

1.5k

头像
括号匹配 题解:
P1501 西北工业大学机试题
发布于2026年1月29日 15:39
阅读数 13

stack模拟过程:左括号入栈,出现右括号取顶判断,模拟过程 

 

#include <iostream>
#include <map>
#include <cctype>   // for isalpha, tolower
#include <string>
#include<algorithm>
#include<stack> 
using namespace std;


int main() {
    string s;
    cin >> s;
    stack<char> st;
    bool valid = true;

    for (int i = 0; i < s.length(); i++) {
        char c = s[i];
        if (c == '(' || c == '[' || c == '{' || c == '<') {
            st.push(c);
        }
        else if (c == ')' || c == ']' || c == '}' || c == '>') {
            if (st.empty()) {
                valid = false;
          ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发