Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <string>
- using std::cin;
- using std::cout;
- using std::stack;
- using std::string;
- int main() {
- stack<int> bank;
- string str;
- cin >> str;
- int len = str.size();
- int bal = 0;
- bool ok = true;
- for (int i = 0; i < len; ++i) {
- char lt = str[i];
- char pre = '*';
- if (!bank.empty()) {
- pre = bank.top();
- }
- if (lt == ')') {
- bal--;
- if (pre == '(') {
- bank.pop();
- } else {
- ok = false;
- break;
- }
- } else if (lt == ']') {
- bal--;
- if (pre == '[') {
- bank.pop();
- } else {
- ok = false;
- break;
- }
- } else if (lt == '}') {
- bal--;
- if (pre == '{') {
- bank.pop();
- } else {
- ok = false;
- break;
- }
- } else {
- bal++;
- bank.push(lt);
- }
- }
- if (bal != 0) {
- ok = false;
- }
- if (ok) {
- cout << "YES\n";
- } else {
- cout << "NO\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement