#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<sstream>
using namespace std;
#define L 101
int top=0;
char a[L]; //栈
char b;
int len = 0;
bool flag = true;
int i=0; //升级变量
char pushA[L][L]={{},{'<'},{'<','('},{'<','(','['}}; //
void push(int n)
{
for(int i=0;i<n;i++){
//遍历检查
if(a[top]==pushA[n][i]){
flag = false;
return;
}
}
a[++top] = b;
//a[++top] = b[i];
}
char popA[L+50];
void pop(){
if(a[top]!=popA[int(b)]) flag = false;
else top--;
}
int main()
{
//输入数据
cin >> len;
//初始化map
popA[int('>')] = '<';
popA[int(')')] = '(';
popA[int(']')] = '[';
popA[int('}')] = '{';
for(i=0;i<len;i++){
cin >> b; //覆盖字符
if(flag){
//也可以用map
switch(b){
case '<':
push(0);break;
case '(':
push(1);break;
case '[':
push(2);break;
case '{':
push(3);break;
default:{
pop();
}
}
}else{
cout << "NO";
break;
}
}//上面还未有结果 => 还可能有剩余
if(flag){
if(top>0) cout << "NO";
else cout << "YES";
}
return 0;
}
//cout << "输入:"
结果:
