Cod sursa(job #3126828)

Utilizator victorstefan28Cucu Victor Stefan victorstefan28 Data 6 mai 2023 23:51:51
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <stack>
#include <fstream>
#include <string>
using namespace std;
ifstream in("paranteze.in");
ofstream out("paranteze.out");
stack<int> s;

bool is_deschidere(char x)
{
    return x == '(' || x == '{' || x == '[';
}
bool is_inchidere(char x)
{
    return x == ')' || x == '}' || x == ']';
}
bool close_parant(char x, char y)
{
    return (x=='(' && y==')') || (x=='[' && y == ']') || (x=='{' && y == '}');
}

int main()
{
    int n;
    string sir;
    in>>n;
    in>>sir;
    int mx = 0, ct = 0;
    for(int i = 0; i<=n; i++)
    {
        if(is_deschidere(sir[i]))
            s.push(i);
        else
        {
            if(s.empty())
                continue;
            if(close_parant(sir[s.top()], sir[i]))
            {
                s.pop();
                if(s.empty())
                    ct=i+1;
                else
                    ct = i-s.top();
                if(ct>mx)
                    mx = ct;
            }
            else
                s.push(i);
        }
    }
    out<<mx;
    return 0;
}