智障的一开始竟然想用递归/记忆化做。。。老T。后来猛然醒悟是栈。。。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<stack> #include<vector> #include<typeinfo> using namespace std; void E() { cout << "No"; exit(0); } void S() { cout << "Yes"; exit(0); } stack<char> s; int main() { char c; while (cin >> c) { switch (c) { case '(':case '[':case '{': s.push(c); break; case ')':case ']':case '}': if (s.empty())E(); switch (c) { case ')': if (s.top() == '(')s.pop(); else E(); break; case ']': if (s.top() == '[')s.pop(); else E(); break; case '}': if (s.top() == '{')s.pop(); else E(); break; } } } S(); } |
http://bjutacm.openjudge.cn/lianxi/1036/