#include<iostream> #include<string> #include<algorithm> #include<stack> using namespace std; //pop前记得判断是否为空 int main() { int t; string s; cin >> t; while (t--) { cin >> s; if (s.length() % 2 != 0) cout << "No" << endl; else { bool judge = true; stack<char> stk; for (int i = 0; i < s.length(); i++) { if (s[i] == '(') stk.push(s[i]); else { if (!stk.empty() && stk.top() == '(') stk.pop(); else { judge = false; break; } } } if (!stk.empty()) judge = false; if (judge) cout << "Yes" << endl; else cout << "No" << endl; } } return 0; } |
Double click to view unformatted code.