这是一段 WA代码........... #include <iostream> #include <stack> #include<string> using namespace std; void main() { stack<char> c; int T,len; cin >> T; getchar(); string str; while (T--) { getline(cin, str); len=str.length(); if (len % 2 != 0) { cout << "No" << endl; continue; } else if(len == 0) { cout << "Yes" << endl; continue; } if (str[0] == ')' || str[0] == ']') { cout << "No" << endl; continue; } for (int i = 0; i < len; i++) { if (str[i] == '(' || str[i] == '[') c.push(str[i]); else { if (str[i] == ')' && c.top()=='(' || str[i] == ']' && c.top() == '[') c.pop(); else { cout << "No" << endl; continue; } } if (i == len - 1) { if (c.size()) cout << "No" << endl; else cout << "Yes" << endl; } } } }
AC代码: #include<iostream> #include<stdio.h> #include<string> using namespace std; string del(string str,int n) { if (str == "") return str; for (int i = n;i < str.size() - 1;i++) { if ((str[i] == '('&&str[i + 1] == ')') || (str[i] == '['&&str[i + 1] == ']')) { str.erase(i, 2); i = i > 0 ? i - 1 : i; return del(str, i); } } return str; } int main() { int T; cin >> T; getchar(); while (T--) { string str; getline(cin, str); if (del(str,0) == "") cout << "Yes" << endl; else cout << "No" << endl; } }