题目
假设表达式中允许包含两种括号:圆括号和方括号。编写一个算法判断表达式中的括号是否正确配对。
输入由括号构成的字符串,包含”(“、”)“、”[“和”]“。输出如果匹配输出YES,否则输出NO。
样例输入
样例输出
YES
分析
栈
AC代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stack>
#include<string>
using namespace std;
char arr;
int main()
{
stack<char>st;
while(scanf("%c",&arr)&&arr!='\n')
{
if(st.empty())
{
st.push(arr);
continue;
}
if((arr==']'&&st.top()=='[')||(arr==')'&&st.top()=='('))
st.pop();
else
st.push(arr);
}
if(st.empty())
cout<<"YES";
else
cout<<"NO";
}