Cod sursa(job #2783843)

Utilizator pifaDumitru Andrei Denis pifa Data 15 octombrie 2021 10:32:39
Problema Fractii Scor 0
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <string>
#include <stack>

using namespace std;

bool verif(string s)
{
    stack <char> stiva;
    for(auto ch: s)
    {
        if(ch == ')')
        {
            if(stiva.empty() or stiva.top() != '(')
            {
                return false;
            }
            stiva.pop();
        }
        else if(ch == '}')
        {
            if(stiva.empty() or stiva.top() != '{')
            {
                return false;
            }
            stiva.pop();
        }
        else
        {
            stiva.push(ch);
        }
    }
    return stiva.empty();
}

int main()
{
    ifstream cin("paranteze1.in");
    ofstream cout("paranteze1.out");
    string s;
    cin >> s;
    if(!verif(s))
    {
        cout << "-1";
    }
    else
    {
        stack <char> stiva;
        for(auto ch: s)
        {
            if(ch == ')')
            {
                if(stiva.empty() or stiva.top() != '(')
                {
                   stiva.push(ch);
                }
                stiva.pop();
            }
            else if(ch == '}')
            {
                if(stiva.empty() or stiva.top() != '{')
                {
                    stiva.push(ch);
                }
            }
        }
        cout << stiva.size();
    }
    return 0;
}