算法思想:左边匹配右边,如果左边或者是右边多的话都无法匹配成功
一对一,头对当前遍历的点—》栈
acwing括号匹配
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
using namespace std;
string s;
int main() {
stack<int> q;
cin >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
q.push(1);
// cout<<1<<endl;
} else if (s[i] == '{') {
q.push(2); // cout<<2<<endl;
} else if (s[i] == '[') {
q.push(3); //cout<<3<<endl;
} else if (s[i] == '<') {
q.push(4); //cout<<4<<endl;
} else {
if (s[i] == ')') {
if (q.size() == 0 || q.top() != 1) { //cout<<q.front()<<endl;
cout << "no" << endl;
return 0;
} else q.pop();
}
if (s[i] == '}') {
if (q.size() == 0 || q.top() != 2) { //cout<<q.front()<<endl;
cout << "no" << endl;
return 0;
} else q.pop();
}
if (s[i] == ']') {
if (q.size() == 0 || q.top() != 3) { // cout<<q.front()<<endl;
cout << "no" << endl;
return 0;
} else q.pop();
}
if (s[i] == '>') {
if (q.size() == 0 || q.top() != 4) { // cout<<q.front()<<endl;
cout << "no" << endl;
return 0;
} else q.pop();
}
}
}
if (q.size()) {
cout << "no" << endl;
return 0;
}
cout << "yes" << endl;
return 0;
}