天天看點

201612-3 權限查詢 模拟

傳送門

CCF第三題, 同樣是一道非常複雜度模拟題, 要非常細心不要漏掉哪一點.

可惜我隻得了90分, 沒得滿分, 找不出那10分是錯在哪裡了.

貼下90分代碼

#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

void show(map<string, int> orig)
{
    map<string, int>::iterator im = orig.begin();
    while (im != orig.end()) {
        cout << im->first << " : " << im->second << endl;
        im++;
    }
}

int main()
{
    int p;
    cin >> p;
    string category;
    map<string, int> orig; // 儲存所有的權限.(原始) 
    for (int i = , j; i < p; ++i) {
        cin >> category;
        j = category.size();
        if (category[j - ] == ':') {
            string tmp;
            for (int k = ; k < j - ; ++k)
                tmp += category[k];
            orig[tmp] = category[j - ] - '0';
        } else orig[category] = -;
    }
    int r;
    cin >> r;
    string ident, categ;
    map<string, map<string, int> >man;
    for (int i = , num; i < r; ++i) {
        cin >> ident >> num;
        map<string, int> tmp;
        for (int j = ; j < num; ++j) {
            cin >> categ;
            int t = categ.size();
            if (categ[t - ] == ':') {
                string temp;
                for (int k = ; k < t - ; ++k)
                    temp += categ[k];
                tmp[temp] = categ[t - ] - '0';
            } else tmp[categ] = -;
        }
        man[ident] = tmp;
    }
    /*cout << "-----------------------------" << endl;
    map<string, map<string, int> >::iterator im = man.begin();
    while (im != man.end()) {
        cout << im->first << endl;
        show(im->second);
        cout << endl;
        im++;
    }*/
    int u, t;
    cin >> u;
    string name, job;
    map<string, vector<string> > user;
    for (int i = ; i < u; ++i) {
        vector<string> tmp;
        cin >> name;
        cin >> t;
        for (int j = ; j < t; ++j) {
            cin >> job;
            tmp.push_back(job);
        }
        user[name] = tmp;
    }
    /*cout << "----------------------------\n";
    map<string, vector<string> >::iterator iu = user.begin();
    while (iu != user.end()) {
        for (int i = 0; i < iu->second.size(); ++i) {
            cout << iu->second[i] << ' ';
        }
        cout << endl;
        iu++;
    }
    cout << "----------------------------\n";*/
    int q;
    cin >> q;
    for (int i = ; i < q; ++i) {
        cin >> name >> categ;
        string cmd; // 權限名稱 
        int level, l; // 權限等級 
        l = categ.size();
        if (categ[l - ] != ':') cmd = categ, level = -;
        else {
            for (int k = ; k < l - ; ++k)
                cmd += categ[k];
            level = categ[l - ] - '0';
        }
        //cout << "name = " << name << "  cmd = " << cmd << endl;
        if (user.count(name) ==  || orig.count(cmd) == ) {
            cout << "false" << endl;
            continue;
        }
        vector<string> jobs = user[name];
        /*cout << "\n" << name << " : \n";
        for (int j = 0; j < jobs.size(); ++j) {
            cout << jobs[j] << endl;
        }*/
        //cout << "name = " << name << "  cmd = " << cmd << endl;
        if (level == -) { // 查詢本身沒有等級 
            if (orig[cmd] == -) { // 權限本身沒有等級 
                //cout << "again name = " << name << " cmd = " << cmd << endl;
                bool flag = false; 
                for (int j = ; j < jobs.size(); ++j) {
                    string jb = jobs[j]; // cao, 這裡j寫成i 
                    //cout << "jb = " << jb << endl;
                    map<string, int> now = man[jb];
                    if (now.count(cmd) == ) flag = true;
                }
                if (flag) cout << "true" << endl;
                else cout << "false" << endl;
            } else { // 查詢沒有等級,但是實際上有等級 
                int lev = -;
                for (int j = ; j < jobs.size(); ++j) {
                    string jb = jobs[j];
                    map<string, int> now = man[jb];
                    if (now.count(cmd) == ) {
                        lev = max(lev, now[cmd]);
                    }
                }
                if (lev == -) {
                    cout << "false" << endl;
                } else cout << lev << endl;
            } 
        } else {
            int lev = -;
            for (int j = ; j < jobs.size(); ++j) {
                string jb = jobs[j];
                map<string, int> now = man[jb];
                if (now.count(cmd) == ) {
                    lev = max(lev, now[cmd]);
                }
            }
            if (lev >= level && lev != -) cout << "true" << endl;
            else cout << "false" << endl;
        }
    }
}