题解 | Problem E

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

int main() {
    int n;
    while (cin >> n) {
        while (n--) {
            string s;
            cin >> s;
            stack<char>st;
            int flag = 0;
            for (char a : s) {
                if (a == '(' || a == '[' || a == '{') {
                    st.push(a);
                } else if (a == ')' || a == ']' || a == '}') {
                    if (st.empty()) {
                        cout << "no" << endl;
                        flag = 1;
                        break;
                    } else {
                        char c = st.top();
                        if ((a == ')' && c == '(') || (a == ']' && c == '[') || (a == '}' &&
                                c == '{')) {
                            if (st.empty()) {
                                cout << "no" << endl;
                                flag = 1;
                            } else st.pop();
                        } else {
                            cout << "no" << endl;
                            flag = 1;
                            break;
                        }
                    }

                }
            }
            if (flag == 0) {
                if (st.empty())cout << "yes" << endl;
                else cout << "no" << endl;
            }

        }
    }
}

简单匹配,用一下栈即可

全部评论

相关推荐

没有offer的呆呆:薪资有的时候也能说明一些问题,太少了活不活得下去是一方面,感觉学习也有限
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务