题目大意
判断圆括号,方括号是否匹配。
思路
单独分类讨论,不同括号直接return
。
#include <iostream>
#include <stack>
using namespace std;
stack<char> oper;
int main() {
string n;
cin >> n;
for (int i = 0; i < n.size(); i++) {
if (n[i] == '(' || n[i] == '[')
oper.push(n[i]);
if (oper.empty()) {
cout << "Wrong" << endl;
return 0;
}
if (n[i] == ')' || n[i] == ']') {
if (oper.top() == '[' && n[i] == ']' || oper.top() == '(' && n[i] == ')')
oper.pop();
else {
cout << "Wrong" << endl;
return 0;
}
}
}
if (oper.empty())
cout << "OK";
else
cout << "Wrong";
}