Cod sursa(job #1990318)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 11 iunie 2017 13:52:57
Problema Episoade Scor 100
Compilator cpp Status done
Runda Simulare 14a Marime 1.68 kb
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

ifstream fin ("episoade.in"); ofstream fout ("episoade.out");

typedef pair<int, int> pii;

const int nmax = 100;
pii gata = make_pair(-1, -1);

int pos[nmax + 1];

string s;
int ind;
bool ok;

pii perm(), ord();

pii nr() {
    if (s[ ind ] == '(') {
        ++ ind;
        pii ans = perm();
        ++ ind;
        return ans;
    }

    int ans = 0;
    while (s[ ind ] >= '0' && s[ ind ] <= '9') {
        ans = ans * 10 + s[ ind ] - '0';
        ++ ind;
    }
    return make_pair(pos[ ans ], pos[ ans ]);
}

pii perm() {
    vector< pii > v;

    v.push_back( ord() );
    if (ok == 0) return gata;

    while (s[ ind ] == '#') {
        ++ ind;
        v.push_back( ord() );
        if (ok == 0) return gata;
    }

    sort(v.begin(), v.end());

    pii ans = v[ 0 ];
    for (int i = 1; i < (int)v.size(); ++ i) {
        if (v[ i ].first != ans.second + 1) {
            ok = 0; return gata;
        }
        ans.second = v[ i ].second;
    }
    return ans;
}

pii ord() {
    pii ans;

    ans = nr();
    if (ok == 0) return gata;

    while (s[ ind ] == '>') {
        ++ ind;
        pii aux = nr();
        if (ok == 0) return gata;

        if (ans.second + 1 != aux.first) {
            ok = 0; return gata;
        }
        ans.second = aux.second;
    }

    return ans;
}

int main() {
    int t, n;
    fin >> s >> t >> n;

    while (t --) {
        for (int i = 1; i <= n; ++ i) {
            int x;
            fin >> x;
            pos[ x ] = i;
        }

        ind = 0;
        ok = 1;

        perm();

        fout << ok << "\n";
    }

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