Untitled

 avatar
unknown
c_cpp
2 years ago
967 B
3
Indexable
#include <iostream>
#include <vector>
#include <stack>
#include <map>
using namespace std;

int main()
{
    string str;
    stack<char> stack;
    cin >> str;
    // str = "([)]";
    map<char, char> open;
    map<char, char> close;
    open['('] = ')';
    open['['] = ']';
    open['{'] = '}';
    close[')'] = '(';
    close[']'] = '[';
    close['}'] = '{';

    for (size_t i = 0; i < str.length(); ++i)
    {
        if (close.count(str[i])) {
            if (stack.empty()) {
                cout << i;
                return 0;
            }
            else {
                if (stack.top() == close[str[i]]) {
                    stack.pop();
                }
                else {
                    cout << i;
                    return 0;
                }
            }
        }
        else {
            stack.push(str[i]);
        }

    }
    if (stack.empty())
        cout << "CORRECT";
    else
        cout << str.length();
    return 0;
}
Editor is loading...