文章

39

粉丝

0

获赞

80

访问

7.1k

头像
括号的匹配 题解:
P1067 中山大学机试题
发布于2026年1月27日 15:47
阅读数 226

Map定义层级和存储括号对,stack出入匹配括号

 

#include <iostream>
#include <stack>
#include <map>
#include <string>
using namespace std;

int main() {
    int n;
    cin >> n;
    string line;
    getline(cin, line); // consume the newline after n

    // 定义括号层级:越小越外层
    map<char, int> level;
    level['{'] = 0;
    level['['] = 1;
    level['('] = 2;
    level['<'] = 3;

    // 右括号到左括号的映射
    map<char, char> match;
    match['}'] = '{';
    match[']'] = '[';
    match[')'] = '(';
    match['>'] = '<';

    for (int idx = 0; idx < n; ++idx) {
        getline(cin, line);
        stack<char> st;
  ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发