标签:
3 [(]) (]) ([[]()])
No No Yes
#include <iostream> using namespace std; class Stack { private: int top; char s[10001]; public: Stack() { top = -1; } bool empty() { if (top == -1) return true; else return false; } void push(char c) { s[++top] = c; } void pop() { top--; } char get_top() { return s[top]; } int length() { return top; } }; int main() { int T; char s[10001]; cin >> T; int i; char c; while (T--) { cin >> s; Stack stack; for (i = 0;s[i] != ‘\0‘;i++) { if (stack.empty() == true) { stack.push(s[i]); } else { c = stack.get_top(); if (c == ‘[‘) { if (s[i] == ‘]‘) { stack.pop(); } else { stack.push(s[i]); } } else if (c == ‘(‘) { if (s[i] == ‘)‘) { stack.pop(); } else { stack.push(s[i]); } } else { stack.push(s[i]); } } } if (stack.length() == -1) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
标签:
原文地址:http://www.cnblogs.com/zqxLonely/p/4913359.html