1.使用unordered_map来进行括号对应
2.出栈时要判断栈是否为空
#include <iostream>
#include <unordered_map>
#include <stack>
using namespace std;
unordered_map<char,int> mp;
int main()
{
string s;
bool flag = true;
mp = {
{'<',-1},
{'>',1},
{'(',-2},
{')',2},
{'[',-3},
{']',3},
{'{',-4},
{'}',4},
};
stack<int> stk;
int temp;
cin >> s;
for(auto c : s)
{
temp = mp[c];
if(temp < 0)stk.push(temp);
else
{
if(stk.size() && stk.top() == -temp)
stk.pop();
else
{
flag = false;
break;
}
}
}
if(stk.empty() && flag)printf("yes");
else printf("no");
return 0;
}