题解 | 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;
            }

        }
    }
}

简单匹配,用一下栈即可

全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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