题解 | 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; } } } }
简单匹配,用一下栈即可