Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- #include <queue>
- #include <string>
- #include <sstream>
- using namespace std;
- char getMirrorParanthesis(char one) {
- char sign = NULL;
- switch (one) {
- case '{': sign = '}'; break;
- case '(': sign = ')'; break;
- case '[': sign = ']'; break;
- case '}': sign = '{'; break;
- case ')': sign = '('; break;
- case ']': sign = '['; break;
- case ' ': sign = ' '; break;
- }
- return sign;
- }
- int main() {
- string line;
- getline(cin, line);
- stack<char> left;
- queue<char> right;
- for (int i = 0; i < line.size(); i++) {
- char paranthesis = line[i];
- if (i < line.size() / 2) {
- left.push(paranthesis);
- } else {
- right.push(paranthesis);
- }
- }
- bool balanced = true;
- if (left.size() == right.size()) {
- while (left.size() && right.size()) {
- char leftSign = left.top(); left.pop();
- char rightSign = right.front(); right.pop();
- if (getMirrorParanthesis(leftSign) != rightSign) {
- balanced = false;
- break;
- }
- }
- } else {
- balanced = false;
- }
- cout << (balanced ? "YES" : "NO") << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement