括号配对问题
时间限制:3000 ms | 内存限制:65535 KB
难度:3
- 描述
- 现在,有一行括号序列,请你检查这行括号是否配对。
#include<stdio.h> #include<string.h> #include<stack> #include<algorithm> using namespace std; char a[101100]; int main() { int t; scanf("%d",&t); getchar(); while(t--) { scanf("%s",a); int l=strlen(a); if(l%2==1) printf("No "); else { if(a[0]==']'||a[0]==')') printf("No "); else { stack<char>s; for(int i=0;i<l;i++) { if(s.size()==0) s.push(a[i]); else { if(s.top()=='['&&a[i]==']'||s.top()=='('&&a[i]==')') s.pop(); else s.push(a[i]); } } if(!s.empty()) printf("No "); else printf("Yes "); } } } return 0; }