zoj 3002 Software Installer

zoj 3002 Software Installer,第1张

zoj 3002 Software Installer
#include <map>#include <string>#include <vector>#include <cstdio>#include <cstring>using namespace std;vector<vector<int> > dependency;map<string, int> s2i, p2i;vector<string> i2s, i2p;vector<int> sc, pc;void install(int id, bool show = false){    if (sc[id] > 0) {        if (show) { printf("%s has been already installedn", i2s[id].c_str());        }        return;    }    ++sc[id];    if (show) {        printf("install");    }    for (size_t i = 0; i < dependency[id].size(); i++) {        if (dependency[id][i] < 0) { if (sc[-1 - dependency[id][i]] == 0) {     install(-1 - dependency[id][i]);     if (show) {         printf(" %s", i2s[-1 - dependency[id][i]].c_str());     } } ++sc[-1 - dependency[id][i]];        }        else { if (pc[dependency[id][i]] == 0) {     if (show) {         printf(" %s", i2p[dependency[id][i]].c_str());     } } ++pc[dependency[id][i]];        }    }    if (show) {        printf("n");    }}void uninstall(int id, bool show = false){    if (sc[id] == 0) {        if (show) { printf("%s has been already uninstalledn", i2s[id].c_str());        }        return;    }    if (sc[id] > 1) {        if (show) { printf("uninstall failed: %s is used by other softwaren", i2s[id].c_str());        }        return;    }    --sc[id];    if (show) {        printf("uninstall");    }    for (size_t i = 0; i < dependency[id].size(); i++) {        if (dependency[id][i] < 0) { --sc[-1 - dependency[id][i]];        }        else { if (pc[dependency[id][i]] == 1) {     if (show) {         printf(" %s", i2p[dependency[id][i]].c_str());     } } --pc[dependency[id][i]];        }    }    if (show) {        printf("n");    }}int main(void){    static char ch, buf[256];    int ri = 0, ns, np, n;    while (scanf("%d", &ns) != EOF) {        s2i.clear();        i2s.resize(ns);        sc.resize(ns);        for (int i = 0; i < ns; i++) { scanf("%s", buf); s2i[buf] = i; i2s[i] = buf; sc[i] = 0;        }        scanf("%d", &np);        p2i.clear();        i2p.resize(np);        pc.resize(np);        for (int i = 0; i < np; i++) { scanf("%s", buf); p2i[buf] = i; i2p[i] = buf; pc[i] = 0;        }        dependency.resize(ns);        for (int i = 0; i < ns; i++) { scanf("%d", &n); dependency[i].resize(n); for (int j = 0; j < n; j++) {     scanf("%s", buf);     dependency[i][j] = ((s2i.find(buf) == s2i.end()) ? p2i[buf] : (-1 - s2i[buf])); }        }        scanf("%d", &n);        printf("case %d:n", ++ri);        while (n--) { scanf(" %c%*s%s", &ch, buf); if (s2i.find(buf) == s2i.end()) {     printf("%s is not foundn", buf); } else if (ch == 'i') {     install(s2i[buf], true); } else {     uninstall(s2i[buf], true); }        }        putchar('n');    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4936684.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存