提交时间:2023-08-22 16:08:55

运行 ID: 99309

#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int N = 300; const char f[] = {'{','[','(','<','}',']',')','>'}; int T, a[N],sta[N]; int main(){ cin >> T; while(T --){ string s; cin >> s; int lena = s.size(); int top = 0; bool flag1 = true;//最开始默认匹配 for(int i = 0; i < lena ;i++) for(int j = 0; j < 8; j++) if(s[i] == f[j]) { a[i] = j; break; } for(int i = 0; i < lena;i++) if(a[i] <= 3){ if(!top || a[i] >= sta[top]) sta[++top] = a[i]; else { flag1 = false; break; } } else{ if(top && sta[top] + 4 == a[i]) top--; else { flag1 = false; break; } } if(top) cout << "NO\n"; else if(flag1) cout << "YES\n"; else cout << "NO\n"; } return 0; }