1 #include <stdio.h> 2 #include <string.h> 3 #include <stack> 4 using namespace std; 5 6 const int MAX = 256; 7 char str[MAX]; 8 int Map[MAX]; 9 10 11 int main() 12 { 13 #ifdef CDZSC_OFFLINE 14 freopen("in.txt", "r", stdin); 15 freopen("out.txt", "w", stdout); 16 #endif 17 Map[‘(‘] = -1; Map[‘)‘] = 1; 18 Map[‘[‘] = -2; Map[‘]‘] = 2; 19 20 int t, len; 21 scanf("%d", &t); 22 getchar(); 23 while(t--) 24 { 25 gets(str); 26 27 len = strlen(str); 28 stack<char> st; 29 for(int i = 0; i < len; i++) 30 { 31 if(str[i] == ‘(‘ || str[i] == ‘[‘) 32 { 33 st.push(str[i]); 34 } 35 else 36 { 37 if(!st.empty() && !(Map[st.top()] + Map[str[i]])) 38 { 39 st.pop(); 40 } 41 else 42 { 43 st.push(str[i]); 44 } 45 } 46 } 47 printf("%s\n", st.empty() ? "Yes" : "No"); 48 } 49 return 0; 50 }
原文:http://www.cnblogs.com/LiuACG/p/4251334.html