题解 | Problem E

Problem E

https://www.nowcoder.com/practice/3bad4a646b5b47b9b85e3dcb9488a8c3

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

bool isbrackets(char ch) {
    if (ch == '{' || ch == '}' || ch == '(' || ch == ')' || ch == '[' ||
            ch == ']')return true;
    return false;
}

int main() {
    int a;
    string s;
    while (cin >> a ) { // 注意 while 处理多个 case
        for (int i = 0; i < a; i++) {
            cin.ignore();
            cin >> s;
            stack<char>ss;
            bool flag = true;
            for (int j = 0; j < s.length() && flag; j++) {
                if (!isbrackets(s[j]))continue;

                if (s[j] == '[' || s[j] == '{' || s[j] == '(') {
                    ss.push(s[j]);
                } else {
                    if (s[j] == ']') {
                        if (!ss.empty()&&ss.top() == '[')ss.pop();
                        else flag = false;
                    }
                    if (s[j] == ')') {
                        if (!ss.empty()&&ss.top() == '(')ss.pop();
                        else flag = false;
                    }
                    if (s[j] == '}') {
                        if (!ss.empty()&&ss.top() == '{')ss.pop();
                        else flag = false;
                    }
                }

            }
            if (ss.empty()&&flag)cout << "yes" << endl;
            else cout << "no" << endl;
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-03 12:09
東京大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务