zl程序教程

您现在的位置是:首页 >  其它

当前栏目

UVA673

2023-09-27 14:28:12 时间
#include <bits/stdc++.h>

using namespace std;
stack<char> s;
int num;

/**
测试用例:
2
[({})]
()[]()
 */
char trans(char a) {
    if (a == ')') return '(';
    if (a == ']') return '[';
    if (a == '}') return '{';
    return '\0';
}

int main() {
    cin >> num;
    string p;
    //getline(cin, p);//读取末尾的指针
    getchar();
    while (num--) {
        while (!s.empty())s.pop();
        getline(cin, p);
        for (int i = 0; i < p.size(); i++) {
            if (s.empty()) {
                s.push(p[i]);
                continue;
            }
            if (trans(p[i]) == s.top())s.pop();
            else s.push(p[i]);
        }
        if (s.empty())printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}