HDU——2723Electronic Document Security(STL map嵌套set做法)

时间:2022-06-16 06:42:05

Electronic Document Security

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 172    Accepted Submission(s): 94

Problem Description
The Tyrell corporation uses a state-of-the-art electronic document system that controls all aspects of document creation, viewing, editing, and distribution. Document security is handled via access control lists (ACLs). An ACL defines
a set of entities that have access to the document, and for each entity defines the set of rights that it has. Entities are denoted by uppercase letters; an entity might be a single individual or an entire division. Rights are denoted by lowercase letters;
examples of rights are a for append, d for delete, e for edit, and r for read.

The ACL for a document is stored along with that document, but there is also a separate ACL log stored on a separate log server. All documents start with an empty ACL, which grants no rights to anyone. Every time the ACL for a document is changed, a new entry
is written to the log. An entry is of the form ExR, where E is a nonempty set of entities, R is a nonempty set of rights, and x is either "+", "–", or "=". Entry E+R says to grant all the rights in R to all the entities in E, entry E–R says to remove all the
rights in R from all the entities in E, and entry E=R says that all the entities in E have exactly the rights in R and no others. An entry might be redundant in the sense that it grants an entity a right it already has and/or denies an entity a right that
it doesn't have. A log is simply a list of entries separated by commas, ordered chronologically from oldest to most recent. Entries are cumulative, with newer entries taking precedence over older entries if there is a conflict.

Periodically the Tyrell corporation will run a security check by using the logs to compute the current ACL for each document and then comparing it with the ACL actually stored with the document. A mismatch indicates a security breach. Your job is to write a
program that, given an ACL log, computes the current ACL.

Input
The input consists of one or more ACL logs, each 3–79 characters long and on a line by itself, followed by a line containing only "#" that signals the end of the input. Logs will be in the format defined above and will not contain
any whitespace.
Output
For each log, output a single line containing the log number (logs are numbered sequentially starting with one), then a colon, then the current ACL in the format shown below. Note that (1) spaces do not appear in the output; (2) entities
are listed in alphabetical order; (3) the rights for an entity are listed in alphabetical order; (4) entities with no current rights are not listed (even if they appeared in a log entry), so it's possible that an ACL will be empty; and (5) if two or more consecutive
entities have exactly the same rights, those rights are only output once, after the list of entities.
Sample Input
MC-p,SC+c
YB=rde,B-dq,AYM+e
GQ+tju,GH-ju,AQ-z,Q=t,QG-t
JBL=fwa,H+wf,LD-fz,BJ-a,P=aw
#
Sample Output
1:CSc
2:AeBerMeYder
3:
4:BHJfwLPaw

这题是我写过最长的代码题。也是我个大菜鸟写过最复杂的..对于map和set又熟悉了不少。,题意:每一行输入多个names(集合) x rights(集合),name和rights是字符串,x是操作符+、-、=。

举个例子,GOQ=ab,则产生G、O、G三个人(并先清空当前所有权限),每一个人都赋给a与b的权限,题目所给的写法可以视为对一个name集合的每一个字符进行操作,再举个例子,GOQ+ab,则将G、O、Q三个人均加上a与b的权限(若已有则不变)再举个栗子吧GOQ-ab,则将G、O、Q三个*限中均减掉a与b(若原来就没有就不用减)

题目输出要求:对于具有相同权限的人按字典序排序再合并输出,若某人无任何权限,则不输出。因为按字典序,则用map,又由于每一个人的权限重复删除、增加问题,则用set。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<map>
#include<set>
using namespace std;
int main(void)
{
string t,s,name,miaoshu;
char na;
int i,j,k,mid,coma,q,len,p=0;
while (getline(cin,t)&&t!="#")
{
p++;//输入计数器
set<char> right;
map<char,set<char> >list;
map<char,set<char> >::iterator mit,tmit;
set<char>::iterator sit;
map<char,set<char> >::reverse_iterator jmit,kmit;
k=0;
while (t.find(",",k)!=string::npos)//由于结尾无逗号,只能先处理前n-1个短句
{
coma=t.find(",",k);
for (i=k; i<coma; i++)
{
if(t[i]=='-')
{
for (j=k; j<i; j++)
{
for(q=i+1; q<coma; q++)
{
if(list[t[j]].count(t[q])!=0)
list[t[j]].erase(t[q]);
}
}
break;
}
else if(t[i]=='+')
{
for (j=k; j<i; j++)
{
for(q=i+1; q<coma; q++)
{
if(list[t[j]].count(t[q])==0)
list[t[j]].insert(t[q]);
}
}
break;
}
else if(t[i]=='=')
{
for (j=k; j<i; j++)
{
list[t[j]].clear();
for(q=i+1; q<coma; q++)
{
list[t[j]].insert(t[q]);
}
}
break;
}
}
k=coma+1;
}
//开始处理最后一个句子
len=t.size();
for (i=k; i<len; i++)
{
if(t[i]=='-')
{
for (j=k; j<i; j++)
{
for(q=i+1; q<len; q++)
{
if(list[t[j]].count(t[q])!=0)
list[t[j]].erase(t[q]);
}
}
break;
}
else if(t[i]=='+')
{
for (j=k; j<i; j++)
{
for(q=i+1; q<len; q++)
{
if(list[t[j]].count(t[q])==0)
list[t[j]].insert(t[q]);
}
}
break;
}
else if(t[i]=='=')
{
for (j=k; j<i; j++)
{
list[t[j]].clear();
for(q=i+1; q<len; q++)
{
list[t[j]].insert(t[q]);
}
}
break;
}
}
//处理完毕
for (mit=list.begin(); mit!=list.end(); mit++)//找出没有权限的人,抹掉他
{
if((mit->second).empty())
list.erase(mit++);
}
printf("%d:",p);
for (mit=list.begin(); mit!=list.end(); mit++)
{
tmit=mit;
tmit++;
if((tmit->second)!=(mit->second)&&!(mit->second).empty())//若第n个与第n-1个权限不相同且权限不为空,则说明不用合并
{
cout<<mit->first;
for(sit=(mit->second).begin(); sit!=(mit->second).end(); sit++)
cout<<*sit;
}
else if(!(mit->second).empty())//若权限相同且不为空则只要输出名字即可
cout<<mit->first;
}
printf("\n");
}
return 0;
}