题目大意:给出几种操作
PUSH:在栈顶压入一个空集
DUP:把栈顶的集合复制一下在放入栈顶
ADD:取出栈顶两个集合相加,再把结果放入栈顶
UNION:取出栈顶两个集合,取这两个集合的并集并放入栈顶
INTERSET:取出栈顶两个集合,取这两个集合的交集并放入栈顶
解题思路:用STL
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef set<int> Set;
map<Set, int> IDcache; vector<Set> Setcache;
int ID(Set x) {
if(IDcache.count(x))
return IDcache[x];
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - 1;
}
int main() {
int test, n;
char str[20];
scanf("%d", &test);
while(test--) {
stack<int> s;
int n;
scanf("%d", &n);
while(n--) {
scanf("%s", str);
if(strcmp(str,"PUSH") == 0) {
s.push(ID(Set()));
}
else if(strcmp(str,"DUP") == 0)
s.push(s.top());
else {
Set x1 = Setcache[s.top()];
s.pop();
Set x2 = Setcache[s.top()];
Set x;
s.pop();
if(strcmp(str,"UNION") == 0)
set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
if(strcmp(str,"INTERSECT") == 0)
set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
if(strcmp(str,"ADD") == 0) {
x = x2;
x.insert(ID(x1));
}
s.push(ID(x));
}
printf("%d\n",Setcache[s.top()].size());
}
printf("***\n");
}
return 0;