Cod sursa(job #2889697)

Utilizator Iolanda08Iolanda Caliman Iolanda08 Data 13 aprilie 2022 02:44:04
Problema Deque Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
ifstream fin("paranteze.in");
ofstream fout("paranteze.out");

stack<char>s1;
stack<int>s2;
int n, nrmax;
char x;
int main()
{
    fin>>n;
    s1.push('0');
    s2.push(0);
    int i;
    for(i=1; i<=n; i++)
    {
        fin>>x;
        if(s1.top() == '(' && x ==')' || s1.top() == '[' && x ==']'||s1.top() == '{' && x =='}')
        {
            if(!s1.empty())
			{
				s1.pop();
				s2.pop();
			}
			if(i -s2.top()> nrmax)
                nrmax=i=s2.top();
        }
        else
        {
            s1.push(x);
            s2.push(i);
        }
    }

    fout<<nrmax;
    fin.close();
    fout.close();
    return 0;
}