ccf 201612-3 权限查询
ccf 201612-3 权限查询
解题思路:
建立一个二维矩阵存储权限和角色
还差30分emmm

1 #include<iostream> 2 #include<cstring> 3 #include<map> 4 using namespace std; 5 const int maxn = 100+5; 6 const int maxq = 10000 + 5; 7 map<string,int> cl;///权限名称到最高权限级别的映射 8 map<string,int> cn;//权限名称到数组下标的转换 9 map<string,int> rn;//角色名称到数组下标的转换 10 map<string,int> un;//用户名称到数组下标的转换 11 int rc[maxn][maxn];///rc[i][j]表示当前角色i拥有的权限j的最高等级 12 bool ur[maxn][maxn];///用户i是否有 角色j 13 int main() 14 { 15 char str[maxq]; 16 int p,r,u,q; 17 cin>>p; 18 for(int i=0;i<p;i++) 19 { 20 cin>>str; 21 char *find = strchr(str,':'); 22 if(find == NULL)//无等级权限 23 { 24 cl[str] = -1; 25 cn[str] = i; 26 } else{ 27 int len = find - str; 28 char c[maxn]; 29 int j = 0; 30 for(j=0;j<len;j++) 31 { 32 c[j] = str[j]; 33 } 34 c[j] = '\0'; 35 int num = 0; 36 for(j=len+1;j<strlen(str);j++) 37 { 38 num = num*10 + (str[j] - '0'); 39 } 40 cl[c] = num; 41 cn[c] = i; 42 } 43 } 44 cin>>r;//描述角色 45 for(int i=0;i<r;i++) 46 for(int j=0;j<p;j++) 47 rc[i][j] = -2; 48 for(int i=0;i<r;i++) 49 { 50 char str[maxn]; 51 cin>>str; 52 rn[str] = i; 53 int s; 54 cin>>s; 55 for(int j=0;j<s;j++){//读入角色拥有的权限 56 char c[maxn]; 57 cin>>c; 58 char *find = strchr(c,':'); 59 if(find == NULL){ 60 //无等级权限 61 rc[rn[str]][cn[c]] = -1; 62 }else{ 63 int len = find - c; 64 char temp[maxn]; 65 int j = 0; 66 for(j=0;j<len;j++) 67 { 68 temp[j] = c[j]; 69 } 70 temp[j] = '\0'; 71 int num = 0; 72 for(j=len+1;j<strlen(c);j++) 73 { 74 num = num*10 + (c[j] - '0'); 75 } 76 rc[rn[str]][cn[temp]] = num; 77 } 78 } 79 } 80 81 cin>>u;//用户 82 memset(ur,false,sizeof(ur)); 83 for(int i=0;i<u;i++) 84 { 85 char str[maxn]; 86 cin>>str; 87 un[str] = i; 88 int s; 89 cin>>s; 90 for(int j=0;j<s;j++) 91 { 92 char c[maxn]; 93 cin>>c; 94 ur[un[str]][rn[c]] = true; 95 } 96 } 97 98 cin>>q; 99 for(int i=0;i<q;i++) 100 { 101 char user[maxn],c[maxn]; 102 cin>>user>>c; 103 char *find = strchr(c,':'); 104 int num = 0;char temp[maxn]; 105 if(find == NULL){ 106 map<string,int>::iterator iter = cn.find(c); 107 if(iter == cn.end()) 108 { 109 cout<<"flase"<<endl; 110 continue; 111 } 112 iter = un.find(user); 113 if(iter == un.end()) 114 { 115 cout<<"false"<<endl; 116 continue; 117 } 118 }else{ 119 int len = find - c; 120 int j = 0; 121 for(j=0;j<len;j++) 122 { 123 temp[j] = c[j]; 124 } 125 temp[j] = '\0'; 126 127 map<string,int>::iterator iter = cn.find(temp); 128 if(iter == cn.end()) 129 { 130 cout<<"flase"<<endl; 131 continue; 132 } 133 for(j=len+1;j<strlen(c);j++) 134 { 135 num = num*10 + (c[j] - '0'); 136 } 137 iter = un.find(user); 138 if(iter == un.end()) 139 { 140 cout<<"false"<<endl; 141 continue; 142 } 143 } 144 145 if(find ==NULL && cl[c] == -1) 146 { 147 bool flag = false; 148 for(int j=0;j<r;j++)//便利所有的角色 149 { 150 if(ur[un[user]][j]) 151 { 152 if(rc[j][cn[c]] == -1) 153 { 154 cout<<"true"<<endl; 155 flag = true; 156 break; 157 } 158 } 159 } 160 if(!flag) cout<<"false"<<endl; 161 } 162 else if(find ==NULL && cl[c] != -1){ 163 //有等级权限,且查询等级 164 bool flag = false; 165 int max = 0; 166 for(int j=0;j<r;j++)//便利所有的角色 167 { 168 if(ur[un[user]][j]) 169 { 170 if(rc[j][cn[c]] != -2) 171 { 172 if(rc[j][cn[c]] > max) max = rc[j][cn[c]]; 173 flag = true; 174 } 175 } 176 } 177 if(!flag) cout<<"false"<<endl; 178 else cout<<max<<endl; 179 } 180 else if(find != NULL){ 181 182 bool flag = false; 183 for(int j=0;j<r;j++)//便利所有的角色 184 { 185 if(ur[un[user]][j]) 186 { 187 if(rc[j][cn[temp]] >= num) 188 { 189 cout<<"true"<<endl; 190 flag = true; 191 break; 192 } 193 } 194 } 195 if(!flag) cout<<"false"<<endl; 196 } 197 else{ 198 cout<<"false"<<endl; 199 } 200 } 201 202 return 0; 203 }