Rezultati

Up. imeNalogaJezikRezultatČas oddaje
finalsolution-2018 Birokrati C++ 100/100OK 04. okt '18 @ 18:26

Test Točke Porabljen spomin Porabljen čas Status
#1 9/9 2,734 MiB 0,000 s OK
#2 9/9 2,883 MiB 0,000 s OK
#3 9/9 2,734 MiB 0,000 s OK
#4 9/9 2,883 MiB 0,004 s OK
#5 9/9 2,766 MiB 0,000 s OK
#6 9/9 3,020 MiB 0,004 s OK
#7 9/9 3,301 MiB 0,009 s OK
#8 9/9 3,953 MiB 0,004 s OK
#9 9/9 3,816 MiB 0,016 s OK
#10 9/9 8,594 MiB 0,045 s OK
#11 10/10 2,738 MiB 0,000 s OK

Ocenjevani program (birokrati.cpp):
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>

struct leaf {
    struct level *parent;
    std::string name;
    std::string next;
    std::string idn;
};

struct level {
    struct level *parent;
    std::string name;
    std::map<std::string, level> nested;
    std::map<std::string, leaf> leafs;
};

static struct level R;
static std::map<std::string, leaf*> P;
static char tmp[320];

static void recurse(struct level *rp, std::string prefix) {
    int i = 1, j = 1;
    for (auto it = rp->nested.begin(), end = rp->nested.end(); it != end; it++) {
        recurse(&it->second, prefix + std::to_string(i) + ".");
        i++;
    }
    for (auto it = rp->leafs.begin(), end = rp->leafs.end(); it != end; it++) {
        it->second.idn = prefix + std::to_string(j);
        P[it->second.idn] = &it->second;
        j++;
    }
}

static void printlevel(struct level *lv) {
    if (lv == &R) return;
    printlevel(lv->parent);
    printf("/%s", lv->name.c_str());
}

int main() {
    while (scanf("%s", tmp) == 1) {
        char *pt = strtok(tmp, "/");
        char *np = NULL;
        struct level *rp = &R;
        while ((np = strtok(NULL, "/")) != NULL) {
            struct level *lv = &rp->nested[pt];
            lv->parent = rp;
            lv->name = pt;
            rp = lv;
            pt = np;
        }
        struct leaf *lf = &rp->leafs[pt];
        lf->parent = rp;
        lf->name = pt;
        (void)scanf("%s", tmp);
        lf->next = tmp;
    }
    recurse(&R, "");
    struct level *lv = &R;
    while (lv->leafs.empty()) {
        lv = &lv->nested.begin()->second;
    }
    struct leaf *first = &lv->leafs.begin()->second;
    P.erase(first->idn);
    while (P.count(first->next)) {
        struct leaf *next = P[first->next];
        P.erase(first->next);
        first = next;
    }
    printlevel(first->parent);
    printf("/%s\n", first->name.c_str());
    return 0;
}