• 201612-3 权限查询


    实现

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    #include <sstream>
    #include <algorithm>
    #include <cstdlib>
    
    using namespace std;
    
    struct privilege {
        string privi_name;
        int degree;
    };
    
    class Role {
    public:
        vector<privilege> privis;
        
        bool check_exist(privilege& privi) {
            for (int i = 0;i < privis.size();++i) {
                if (privis[i].privi_name == privi.privi_name) {
                    if (privis[i].degree == -1
                    ||(privis[i].degree != -1 && privi.degree <= privis[i].degree)) {
                        return true;
                    } 
                }
            }
            return false;
        }
    
        int check_degree(privilege& privi) {
            int max = -1;
            for (int i = 0;i < privis.size();++i) {
                if (privis[i].privi_name == privi.privi_name 
                && privis[i].degree > max) {
                    max = privis[i].degree;
                }
            }
            return max;
        }
    
        // 0: no , 1: yes -1: not exist
        int is_degree_privilege(string& privi_name) {
            for (int i = 0;i < privis.size();++i) {
                if (privi_name == privis[i].privi_name) {
                    return (privis[i].degree != -1)?1:0;
                }
            }
            return -1;
        }
    };
    
    class user_privilege {
    public:
    
        vector<Role> roles;
        
        void uses_has_privilege(privilege& privi) {
            int degree_max = -1;
            bool check = false;
            for (int i = 0;i < roles.size();++i) {
                if (roles[i].is_degree_privilege(privi.privi_name) == 0) {
                    bool res = roles[i].check_exist(privi);
                    if (res == true) {
                        cout << "true" << endl;
                        return;
                    }
                } else if (roles[i].is_degree_privilege(privi.privi_name) == 1) {
                    if (privi.degree == -1) {
                        int res = roles[i].check_degree(privi);
                        if (res > degree_max) {
                            degree_max = res;
                        }
                    } else {
                        bool res = roles[i].check_exist(privi);
                        if (res == true) {
                            cout << "true" << endl;
                            return;
                        }
                    }
                } else {
                    continue;
                }
            }
    
            if (degree_max == -1) {
                cout << "false" << endl;
            } else {
                cout << degree_max << endl;
            }
        }
    
    };
    
    map<string, user_privilege> users;
    map<string, privilege> privis_gp;
    map<string, Role> roles_gp; 
    
    int main() {
        int privi_num;
        cin >> privi_num;
    
        for (int i = 0;i < privi_num;++i) {
            string privi_str;
            cin >> privi_str;
    
            int degree_pos = privi_str.find(":");
    
            privilege privi_temp;
            if (degree_pos == -1) {
                privi_temp.privi_name = privi_str;
                privi_temp.degree = -1;
            } else {
                privi_temp.privi_name = privi_str.substr(0,degree_pos);
                privi_temp.degree = atoi(privi_str.substr(degree_pos+1, -1).c_str());
            }
            if (privis_gp.find(privi_temp.privi_name) == privis_gp.end()) {
                privis_gp[privi_temp.privi_name] = privi_temp;
            } else {
                if (privis_gp[privi_temp.privi_name].degree != -1) {
                    privis_gp[privi_temp.privi_name].degree = (privis_gp[privi_temp.privi_name].degree>privi_temp.degree)?privis_gp[privi_temp.privi_name].degree:privi_temp.degree;
                }
            }
        }
    
        int role_num;
        cin >> role_num;
    
        for (int i = 0;i < role_num;++i) {
            string role_name;
            int privi_num;
            cin >> role_name;
            cin >> privi_num;
            
            Role role_temp;
            for (int j = 0;j < privi_num;++j) {
                string privi_str;
                cin >> privi_str;
                int degree_pos = privi_str.find(":");
    
                privilege privi_temp;
                if (degree_pos == -1) {
                    privi_temp.privi_name = privi_str;
                    privi_temp.degree = -1;
                } else {
                    privi_temp.privi_name = privi_str.substr(0,degree_pos);
                    privi_temp.degree = atoi(privi_str.substr(degree_pos+1, -1).c_str());
                }
                
                role_temp.privis.push_back(privi_temp);
            }
            roles_gp[role_name] = role_temp;
        }
    
        int user_name;
        cin >> user_name;
    
        for (int i = 0;i < user_name;++i) {
            string user_name;
            int role_num;
    
            cin >> user_name >> role_num;
            user_privilege user_temp;
            for (int j = 0;j < role_num;++j) {
                string role_name;
                cin >> role_name;
                user_temp.roles.push_back(roles_gp[role_name]);
            }
            users[user_name] = user_temp;
        }
    
        int num;
        cin >> num;
        for (int i = 0;i < num;++i) {
            string user_name;
            string privi_str;
    
            cin >> user_name >> privi_str;
            int degree_pos = privi_str.find(":");
    
            privilege privi_temp;
            if (degree_pos == -1) {
                privi_temp.privi_name = privi_str;
                privi_temp.degree = -1;
            } else {
                privi_temp.privi_name = privi_str.substr(0,degree_pos);
                privi_temp.degree = atoi(privi_str.substr(degree_pos+1, -1).c_str()) ;
            }
    
            if (users.find(user_name) != users.end()) {
                users[user_name].uses_has_privilege(privi_temp);
            } else {
                cout << "false" << endl;
            }
        }
    }
    
  • 相关阅读:
    java基础篇 之 非静态内部类
    java基础篇 之 super关键字的理解
    Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡策略)
    java基础篇 之 接口
    java基础篇 之 构造器内部的多态行为
    Spring Cloud学习 之 Spring Cloud Ribbon(负载均衡器源码分析)
    Spring Cloud学习 之 Spring Cloud Ribbon(执行流程源码分析)
    Spring Cloud 学习 之 Spring Cloud Ribbon(基础知识铺垫)
    计算机网络基础
    Python异常处理
  • 原文地址:https://www.cnblogs.com/amonqsq/p/13599590.html
Copyright © 2020-2023  润新知