题解 | 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")