#include #include #include using namespace std;

bool isValid(string s) { stack st; for (int i = 0; i < s.length(); i++) { if (s[i] == '(' || s[i] == '[') { st.push(s[i]); } else if (s[i] == ')' || s[i] == ']') { if (st.empty()) { return false; } char top = st.top(); if ((s[i] == ')' && top == '(') || (s[i] == ']' && top == '[')) { st.pop(); } else { return false; } } } return st.empty(); }

int main() { int T; cin >> T; cin.ignore(); while (T--) { string s; getline(cin, s); if (isValid(s)) { cout << 'YES' << endl; } else { cout << 'NO' << endl; } } return 0; }

括号匹配校验 - C++ 实现

原文地址: https://www.cveoy.top/t/topic/igtO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录