//输入输出上太恶心了,算法很简单 #include <iostream> #include <string> #include <stack> using namespace std; int main() { int n; scanf("%d",&n); getchar(); //这里去除换行符 while (n--) { char str[130] = {0}; gets(str); //不用C++ string ,因为可能遇到空字符串 bool flag = true; if (str == "") { cout << "Yes" << endl; continue; } size_t i; stack<char> cstack; for (i = 0; i < strlen(str); ++i) { if (str[i] == '(' || str[i] == '[') cstack.push(str[i]); if (str[i] == ')') { if (!cstack.empty() && cstack.top() == '(') cstack.pop(); else { flag = false;break; } } if (str[i] == ']') { if (!cstack.empty() && cstack.top() == '[') cstack.pop(); else { flag = false;break; } } } if (flag && cstack.empty()) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }