Ccf-20161203 -- permission query, ccf-20161203 -- permission

Source: Internet
Author: User

Ccf-20161203 -- permission query, ccf-20161203 -- permission

My idea is to associate a user directly with his or her permissions.
For example:
User Role Permissions
Alicehr crm: 2
Directly changed to: Alice: crm: 2

The question and Code are as follows:

Problem description

Question No: 201612-3
Question Name: Permission Query
Time limit: 1.0 s
Memory limit: 256.0 MB
Problem description: Question description authorization is an indispensable part of various business systems. system users obtain the operation permissions of each module in the system through the authorization mechanism.
The authorization mechanism in this question is designed as follows: each user has several roles and each role has several permissions. For example, if the user david has the manager role and the manager role has the crm: 2 permission, then the user david has the crm: 2 permission, that is, the 2nd Level of the crm permission.
Specifically, both the user name and role name are strings consisting of lowercase letters and cannot exceed 32 characters in length. Permissions can be divided into two categories: hierarchical permissions and hierarchical permissions. A hierarchical permission consists of a permission class name and a permission level, separated by a colon. The permission class name is also a string consisting of lowercase letters and cannot exceed 32 characters in length. The permission level is a number ranging from 0 to 9. A larger number indicates a higher permission level. The system stipulates that if a user has permissions of a certain level, the user will automatically have permissions of a lower level. For example, in the above example, in addition to crm: 2, user david also has the permissions of crm: 1 and crm: 0. No hierarchical permission only has the permission class name and no permission level (and no colon is used to separate permissions ).
The user, role, and permission descriptions in the system are provided. Your program needs to answer multiple queries about users and permissions. Queries can be divided into the following types:
* Query with no hierarchical permissions: if the permission itself is not hierarchical, the query does not specify a level, and whether the query has this permission is returned;
* Hierarchical query with hierarchical permissions: if the permission itself has a hierarchical query, the system returns whether the permission has this level;
* Hierarchical permission-free query: if the permission itself is classified and the query does not contain a level, the system returns a level with the permission. If the permission is not of any level, "No" is returned ". The first line of input is a positive integer.PIndicates the number of different permission categories. NextPA row is called a P segment. Each row has a string that describes each permission. For hierarchical permissions, the format is <category >:< level>, where <category> is the name of the permission class, and <level> is the highest level of the permission class. For non-hierarchical permissions, the string only contains the permission class name.
The next line is a positive integer.R, Indicating the number of different roles. NextRA row is called an R segment. Each row describes a role in the format
<Role> <s> <privilege 1> <privilege 2>... <privilege s>
<Role> is the role name, and <s> indicates the number of permissions that the role has. The following <s> strings describe the permissions of the role, in the same format as the P segment.
The next line is a positive integer.U, Indicating the number of users. NextUA row is called a U segment. Each row describes a user in the format
<User> <t> <role 1> <role 2>... <role t>
<User> indicates the user name and <t> indicates the roles of the user. The following <t> strings describe the role of the user.
The next line is a positive integer.QIndicates the number of permission queries. NextQThe row is called the Q segment. Each row describes an authorization query in the format of <user> <privilege>, which indicates whether the query user <user> has <privilege> permission. If the query permission is a hierarchical permission, you can specify a level in the <privilege> query to check whether the user has the permission of this level. You can also leave a level unspecified, indicates the level to which the user has the permission. For non-hierarchical permissions, you can only query whether the user has this permission. You cannot specify a level in the query. Output FormatQLine. Each action is false, true, or a number. False indicates that the user does not have the corresponding permissions. true indicates that the user has the corresponding permissions. For hierarchical queries without hierarchical permissions, the result is a number indicating that the user has the (highest) level of this permission. If the user does not exist or the query permission is not defined, false is returned. Sample input 3
Crm: 2
Git: 3
Game
4
Hr 1 crm: 2
It 3 crm: 1 git: 1 game
Dev 2 git: 3 game
Qa 1 git: 2
3
Alice 1 hr
Bob 2 it qa
Charlie 1 dev
9
Alice game
Alice crm: 2
Alice git: 0
Bob git
Bob poweroff
Charlie game
Charlie crm
Charlie git: 3
Malice game sample output false
True
False
2
False
True
False
True
The following describes the actual permissions of each user in the case of false:
* User alice has the crm: 2 permission
* User bob has crm: 1, git: 2, and game Permissions
* User charlie has git: 3 and game Permissions
* The user malice is not described, so he does not have any permission to evaluate the use case scale and the agreed scale of the use case:
* 1 ≤P,R,U≤ 100
* 1 ≤Q≤ 10 0000000
* Each user has a maximum of 10 roles, and each role has a maximum of 10 permissions.
Conventions:
* The input ensures validity, including:
1) the permissions in the permission list (R segment) corresponding to the role are all previous (P segment), and the permissions can be repeated. If the permissions with a level are repeated, take the highest level as the standard
2) The roles in the role list (U segment) corresponding to the user appear before (R segment). If multiple roles have permissions of a certain level, the highest level prevails.
3) The user name and permission class name in the query (Q segment) are not guaranteed to appear before (U segment and P segment ).
* The first 20% of evaluation cases have only one role.
* The permissions of the first 50% of evaluation cases are not hierarchical, and no query level is available.
 
# Include <bits/stdc ++. h> using namespace std; int main () {int m, n, mm, nn; map <string, int> Quanlity; cin> m; cin. ignore (); for (int I = 0; I <m; I ++) {string temps; cin> temps; if (temps. find (":")! = String: npos) {string quanlityName = temps. substr (0, temps. find (":"); string quanlityIdx = temps. substr (temps. find (":") + 1, temps. length ()-temps. find (":")-1); Quanlity [quanlityName] = atoi (quanlityIdx. c_str ();} else {Quanlity [temps] =-1; //-1 indicates that this permission is not hierarchical .}} cin> n; cin. ignore (); map <string, map <string, int> Roles; for (int I = 0; I <n; I ++) {string temps; getline (cin, temps); stringstream ss (temps); string roleName = ""; ss> ro LeName; int j; ss> j; map <string, int> subQuanlity; for (int k = 0; k <j; k ++) {string tempss; ss> tempss; if (tempss. find (":")! = String: npos) {string quanlityName = tempss. substr (0, tempss. find (":"); string quanlityIdx = tempss. substr (tempss. find (":") + 1, tempss. length ()-tempss. find (":")-1); if (subQuanlity. find (quanlityName )! = SubQuanlity. end () {if (subQuanlity [quanlityName] <atoi (quanlityIdx. c_str () {subQuanlity [quanlityName] = atoi (quanlityIdx. c_str () ;}} else {subQuanlity [quanlityName] = atoi (quanlityIdx. c_str () ;}} else {subQuanlity [tempss] =-1 ;}} Roles [roleName] = subQuanlity;} cin >> mm; cin. ignore (); map <string, map <string, int> Users; for (int I = 0; I <mm; I ++) {string temps; getline (cin, temps); stringstream ss (temps); string userName = ""; Ss> userName; int j; ss> j; map <string, int> subQuanlity; for (int k = 0; k <j; k ++) {string tempss; ss> tempss; if (Roles. find (tempss )! = Roles. end () {map <string, int> ss = Roles [tempss]; map <string, int >:: iterator it; for (it = ss. begin (); it! = Ss. end (); it ++) {if (subQuanlity. find (it-> first )! = SubQuanlity. end () {// if this permission exists, determine whether to update the permission if (subQuanlity [it-> first] <it-> second) {subQuanlity [it-> first] = it-> second ;}} else {subQuanlity [it-> first] = it-> second ;}}}} users [userName] = subQuanlity;} cin> nn; cin. ignore (); for (int I = 0; I <nn; I ++) {string temps; getline (cin, temps); stringstream ss (temps ); string userName = ""; ss> userName; if (Users. find (userName) = Users. end () {cout <"false" <endl; continue;} string quanlity Str = ""; ss> quanlityStr; if (quanlityStr. find (":")! = String: npos) {string quanlityName = quanlityStr. substr (0, quanlityStr. find (":"); string quanlityIdx = quanlityStr. substr (quanlityStr. find (":") + 1, quanlityStr. length ()-quanlityStr. find (":")-1); if (Users [userName]. find (quanlityName )! = Users [userName]. end () if (Users [userName] [quanlityName]> = atoi (quanlityIdx. c_str () {cout <"true" <endl;} else {cout <"false" <endl ;} elsecout <"false" <endl;} else {if (Quanlity. find (quanlityStr) = Quanlity. end () {cout <"false" <endl; continue;} if (Quanlity [quanlityStr]> = 0) {if (Users [userName]. find (quanlityStr )! = Users [userName]. end () {cout <Users [userName] [quanlityStr] <endl;} else {cout <"false" <endl ;}} else {if (Users [userName]. find (quanlityStr )! = Users [userName]. end () {cout <"true" <endl;} else {cout <"false" <endl ;}}} return 0 ;}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.