权限查询-CCF往届题(结果:90分,超时)

模拟题:认真读题,理解题意(敲黑板!!!) 注意到:权限输入(级别可以重复,即会被抬高,取最高);用户角色,具有相同权限时,级别也取最高;所以又两次取极值过程。

留意:输入时,用scanner取一行字符时,后面一定要继续取一行,否则很难猜测取到什么值。

结果:90分,超时。(PS:已经使用了hash在内的很多方法优化时间,然而没用,有人说选了Java,就要接受时不时的超时一下。截止编辑博客时)

试题编号: 201612-3
试题名称: 权限查询
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  授权 (authorization) 是各类业务系统不可缺少的组成部分,系统用户通过授权机制获得系统中各个模块的操作权限。
  本题中的授权机制是这样设计的:每位用户具有若干角色,每种角色具有若干权限。例如,用户 david 具有 manager 角色,manager 角色有 crm:2 权限,则用户 david 具有 crm:2 权限,也就是 crm 类权限的第 2 等级的权限。
  具体地,用户名和角色名称都是由小写字母组成的字符串,长度不超过 32。权限分为分等级权限和不分等级权限两大类。分等级权限由权限类名和权限等级构成,中间用冒号“:”分隔。其中权限类名也是由小写字母组成的字符串,长度不超过 32。权限等级是一位数字,从 0 到 9,数字越大表示权限等级越高。系统规定如果用户具有某类某一等级的权限,那么他也将自动具有该类更低等级的权限。例如在上面的例子中,除 crm:2 外,用户 david 也具有 crm:1 和 crm:0 权限。不分等级权限在描述权限时只有权限类名,没有权限等级(也没有用于分隔的冒号)。
  给出系统中用户、角色和权限的描述信息,你的程序需要回答多个关于用户和权限的查询。查询可分为以下几类:
  * 不分等级权限的查询:如果权限本身是不分等级的,则查询时不指定等级,返回是否具有该权限;
  * 分等级权限的带等级查询:如果权限本身分等级,查询也带等级,则返回是否具有该类的该等级权限;
  * 分等级权限的不带等级查询:如果权限本身分等级,查询不带等级,则返回具有该类权限的等级;如果不具有该类的任何等级权限,则返回“否”。
输入格式
  输入第一行是一个正整数 p,表示不同的权限类别的数量。紧接着的 p 行被称为 P 段,每行一个字符串,描述各个权限。对于分等级权限,格式为 <category>:<level>,其中 <category> 是权限类名,<level> 是该类权限的最高等级。对于不分等级权限,字符串只包含权限类名。
  接下来一行是一个正整数 r,表示不同的角色数量。紧接着的 r 行被称为 R 段,每行描述一种角色,格式为
  <role> <s> <privilege 1> <privilege 2> ... <privilege s>
  其中 <role> 是角色名称,<s> 表示该角色具有多少种权限。后面 <s> 个字符串描述该角色具有的权限,格式同 P 段。
  接下来一行是一个正整数 u,表示用户数量。紧接着的 u 行被称为 U 段,每行描述一个用户,格式为
  <user> <t> <role 1> <role 2> ... <role t>
  其中 <user> 是用户名,<t> 表示该用户具有多少种角色。后面 <t> 个字符串描述该用户具有的角色。
  接下来一行是一个正整数 q,表示权限查询的数量。紧接着的 q 行被称为 Q 段,每行描述一个授权查询,格式为 <user> <privilege>,表示查询用户 <user> 是否具有 <privilege> 权限。如果查询的权限是分等级权限,则查询中的 <privilege> 可指定等级,表示查询该用户是否具有该等级的权限;也可以不指定等级,表示查询该用户具有该权限的等级。对于不分等级权限,只能查询该用户是否具有该权限,查询中不能指定等级。
输出格式
  输出共 q 行,每行为 false、true,或者一个数字。false 表示相应的用户不具有相应的权限,true 表示相应的用户具有相应的权限。对于分等级权限的不带等级查询,如果具有权限,则结果是一个数字,表示该用户具有该权限的(最高)等级。如果用户不存在,或者查询的权限没有定义,则应该返回 false。
样例输入
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
样例输出
false
true
false
2
false
true
false
true
false
样例说明
  样例输入描述的场景中,各个用户实际的权限如下:
  * 用户 alice 具有 crm:2 权限
  * 用户 bob 具有 crm:1、git:2 和 game 权限
  * 用户 charlie 具有 git:3 和 game 权限
  * 用户 malice 未描述,因此不具有任何权限
评测用例规模与约定
  评测用例规模:
  * 1 ≤ p, r, u ≤ 100
  * 1 ≤ q ≤ 10 000
  * 每个用户具有的角色数不超过 10,每种角色具有的权限种类不超过 10
  约定:
  * 输入保证合法性,包括:
  1) 角色对应的权限列表(R 段)中的权限都是之前(P 段)出现过的,权限可以重复出现,如果带等级的权限重复出现,以等级最高的为准
  2) 用户对应的角色列表(U 段)中的角色都是之前(R 段)出现过的,如果多个角色都具有某一分等级权限,以等级最高的为准
  3) 查询(Q 段)中的用户名和权限类名不保证在之前(U 段和 P 段)出现过
  * 前 20% 的评测用例只有一种角色
  * 前 50% 的评测用例权限都是不分等级的,查询也都不带等级

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;


public class Main{
	public static void main(String[] args){
		Scanner in=new Scanner(System.in);
		int ncate=Integer.parseInt(in.nextLine());

		ArrayList<cate> cates=new ArrayList<cate>(ncate);
		HashMap<String,Integer> yingshecates=new HashMap<String,Integer>(ncate);

		for(int i=0;i<ncate;i++){
			String temp=in.nextLine();
			cate tempcate=null;

			if(temp.contains(":")){
				String[] quanli=temp.split(":");
				tempcate=new cate(quanli[0],Integer.parseInt(quanli[1]));
				cates.add(i,tempcate);

				if(!yingshecates.containsKey(quanli[0])){
					yingshecates.put(quanli[0], i);
				}else{
					int index=yingshecates.get(quanli[0]);
					int level=cates.get(index).level;
					if(Integer.parseInt(quanli[1])>level){
						yingshecates.put(quanli[0], i);
					}
				}
			}else{
				tempcate=new cate(temp,-1);
				cates.add(i,tempcate);
				yingshecates.put(temp, i);
			}	
		}


		int nrole=Integer.parseInt(in.nextLine());

		ArrayList<role> roles=new ArrayList<role>(nrole);
		HashMap<String,Integer> yingsheroles=new HashMap<String,Integer>(nrole);

		for(int i=0;i<nrole;i++){
			String temp=in.nextLine();
			String[] zhonglei=temp.split(" ");
			role temprole=new role(zhonglei[0],Integer.parseInt(zhonglei[1]));
			roles.add(temprole);
			cate tempcate=null;
			for(int j=2; j<Integer.parseInt(zhonglei[1])+2; j++){
				if(zhonglei[j].contains(":")){
					String[] quanli=zhonglei[j].split(":");
					tempcate=new cate(quanli[0],Integer.parseInt(quanli[1]));
					if(!temprole.cateyingshe.containsKey(quanli[0])){
						temprole.cateyingshe.put(quanli[0], j-2);

					}else{
						int index=temprole.cateyingshe.get(quanli[0]);
						int level=temprole.category.get(index).level;
						if(level<Integer.parseInt(quanli[1])){
							temprole.cateyingshe.put(quanli[0], j-2);
						}		
					}
				}else{
					tempcate=new cate(zhonglei[j],-1);
					temprole.cateyingshe.put(zhonglei[j], j-2);
				}

				temprole.category.add(j-2,tempcate);
			}
			yingsheroles.put(zhonglei[0], i);
		}


		int nuser=Integer.parseInt(in.nextLine());
		ArrayList<user> users=new ArrayList<user>(nuser);
		HashMap<String,Integer> yingsheusers=new HashMap<String,Integer>(nuser);

		for(int i=0;i<nuser;i++){
			String temp=in.nextLine();
			String[] zhonglei=temp.split(" ");
			user tempuser=new user(zhonglei[0],Integer.parseInt(zhonglei[1]));	
			users.add(tempuser);

			for(int j=2; j<Integer.parseInt(zhonglei[1])+2; j++){
				int index=yingsheroles.get(zhonglei[j]);	
				role tempcate=roles.get(index);
				tempuser.rolearray.add(tempcate);	
			}
			yingsheusers.put(zhonglei[0], i);
		}

		int chaxun=Integer.parseInt(in.nextLine());
		for(int i=0;i<chaxun;i++){
			String temp=in.nextLine();
			String[] zhonglei=temp.split(" ");

			if(!yingsheusers.containsKey(zhonglei[0])){
				System.out.println("false");
			}else{
				String quanliming=null;
				String[] quanli=new String[2];
				boolean flag=true;//true means the quert without power level

				if(zhonglei[1].contains(":")){
					quanli=zhonglei[1].split(":");
					quanliming=quanli[0];
					flag=false;			
				}else{
					quanliming=zhonglei[1];
				}


				if(!yingshecates.containsKey(quanliming)){
					System.out.println("false");
				}else{
					int index=yingsheusers.get(zhonglei[0]);
					user tempuser=users.get(index);

					boolean flag1=true;//the query is not been processed
					int max=Integer.MIN_VALUE;//the max power level
					boolean issatif=false;

					for(role e:tempuser.rolearray){
						if(!e.cateyingshe.containsKey(quanliming)){
							continue;
						}else{
							flag1=false;
							int lookforquanli=e.cateyingshe.get(quanliming);
							cate f=e.category.get(lookforquanli);
							if(flag==true){
								if(max<f.level)max=f.level;
								if(max==-1)break;
							}else{	
								if(Integer.parseInt(quanli[1])<=f.level){
									issatif=true;
									break;
								}
							}	
						}
					}

					if(flag1==true){
						System.out.println("false");
					}else{
						if(flag==true){
							if(max==-1){
								System.out.println("true");
							}else{
								System.out.println(max);
							}
						}else {
							System.out.println(issatif);
						}	
					}
					
					
					flag1=true;//the query is not been processed
					max=Integer.MIN_VALUE;//the max power level
					issatif=false;
					flag=true;//true means the quert without power level
				}
			}
		}
	}
}


class cate{
	String Cname=null;
	int level=-1;
	public cate(String name,int level){
		this.Cname=name;
		this.level=level;
	}
}

class role{
	String Rname=null;
	int num=0;
	ArrayList<cate> category=null;
	HashMap<String,Integer> cateyingshe=null;

	public role(String name,int num){
		this.Rname=name;
		this.num=num;
		category=new ArrayList<cate>(this.num);
		cateyingshe=new HashMap<String,Integer>(this.num);
	}
}

class user{
	public Object roles;
	String Uname=null;
	int num=0;
	ArrayList<role> rolearray=null;
	public user(String name,int num){
		this.Uname=name;
		this.num=num;
		rolearray=new ArrayList<role>(this.num);
	}
}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值