括号匹配
作者:
zzu
,
2024-04-03 20:39:18
,
所有人可见
,
阅读 2
#include <iostream>
#include <unordered_map>
#include <stack>
using namespace std;
unordered_map<char, int> mp{
{'(', -1},
{')', 1},
{'{', -2},
{'}', 2},
{'[', -3},
{']', 3},
{'<', -4},
{'>', 4}
};
int main()
{
stack<char> op;
string s;
cin >> s;
bool c = true;
for(int i = 0; i < s.size(); i ++)
{
int t = mp[s[i]];
if(t < 0)
op.push(t);
else
{
if(op.size() && op.top() == -t)//首先判断栈是否有元素
op.pop();
else
{
c = false;
break;
}
}
}
if(op.size()) c = false;
if(c) cout << "yes" << endl;
else cout << "no" << endl;
return 0;
}