Cod sursa(job #2798716)

Utilizator mediocrekarmaChirvasa George Matei mediocrekarma Data 11 noiembrie 2021 19:25:39
Problema Subsecventa de suma maxima Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;
ifstream fin ("paranteze.in");
ofstream fout ("paranteze.out");


bool openParanthesis(char current) {
    return current == '{' || current == '[' || current == '(';
}
bool match(char opening, char ending) {
    return ending - opening == 1 || ending - opening == 2;
}
int main() {
    fin.tie(NULL);
    std::ios_base::sync_with_stdio(false);
    stack<char> parantheses;
    char current;
    int n, index = 0, maxParanthesized = 0;
    fin >> n;
    fin.get();
    while (n--) {
        fin.get(current);
        if (openParanthesis(current)) {
            parantheses.push(current);
            ++index;
        } else if(!parantheses.empty() && !openParanthesis(current) && n){
            if(match(parantheses.top(), current)) {
                parantheses.pop();
                ++index;
                maxParanthesized = max(maxParanthesized, index - (int)parantheses.size());
            } else {
                parantheses = stack<char>();
                index = 0;
            }
        }
    }
    if (!parantheses.empty() && !openParanthesis(current) && match(parantheses.top(), current)) {
            ++index;
            parantheses.pop();
        if (parantheses.empty()) {
            maxParanthesized = max(maxParanthesized, index - (int)parantheses.size());
        }
    }
    fout << maxParanthesized;
}