题干:

Background from Wikipedia: 揝et theory is a branch of mathematics created principally by the German mathematician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play the role of a foundational theory in modern mathematics, in the sense of a theory invoked to justify assumptions made inmathematics concerning the existence of mathematical objects (such as numbers or functions) and their properties. Formal versions of set theory also have a foundational role to play as specifying a theoretical ideal of mathematical rigor in proofs.?Given this importance of sets, being the basis of mathematics, a set of eccentric theorist set off to construct a supercomputer operating on sets instead of numbers. The initial Set-Stack Alpha is under construction, and they need you to simulate it in order to verify the operation of the prototype. 

 

The computer operates on a single stack of sets, which is initially empty. After each operation, the cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted |S| and is the number of elements in S. The instruction set of the SetStack Alpha is PUSH, DUP, UNION, INTERSECT, and ADD. 
?PUSH will push the empty set {} on the stack. 
?DUP will duplicate the topmost set (pop the stack, and then push that set on the stack twice). 
?UNION will pop the stack twice and then push the union of the two sets on the stack. 
?INTERSECT will pop the stack twice and then push the intersection of the two sets on the stack. 
?ADD will pop the stack twice, add the first set to the second one, and then push the resulting set on the stack. 
For illustration purposes, assume that the topmost element of the stack is A = {{}, {{}}} and that the next one is B = {{}, {{{}}}}. 
For these sets, we have |A| = 2 and |B| = 2. Then: 
?UNION would result in the set { {}, {{}}, {{{}}} }. The output is 3. 
?INTERSECT would result in the set { {} }. The output is 1. 
?ADD would result in the set { {}, {{{}}}, {{},{{}}} }. The output is 3. 

Input

An integer 0 <= T <= 5 on the first line gives the cardinality of the set of test cases. The first line of each test case contains the number of operations 0 <= N <= 2 000. Then follow N lines each containing one of the five commands. It is guaranteed that the SetStack computer can execute all the commands in the sequence without ever popping an empty stack.

Output

For each operation specified in the input, there will be one line of output consisting of a single integer. This integer is the cardinality of the topmost element of the stack after the corresponding command has executed. After each test case there will be a line with *** (three asterisks).

Sample Input

2
9
PUSH
DUP
ADD
PUSH
ADD
DUP
ADD
DUP
UNION
5
PUSH
PUSH
ADD
PUSH
INTERSECT

Sample Output

0
0
1
0
1
1
2
2
2
***
0
0
1
0
0
***

题目大意:集合的运算
有5种操作:

PUSH: 空集“{}”入栈。

DUP:把当前栈顶元素复制一份后再入栈。

UNION:出栈两个集合,然后把二者的并集入栈。

INTERSECT:出栈两个集合,然后把二者的交集入栈

ADD:出栈两个集合,然后把先出栈的加入到后出栈的集合中,把结果入栈。

每次操作后输出栈顶集合的大小。

解题报告:

     是集合的集合,为了表示不同的集合,可用一个整型ID表示,比如说用1表示{},集合{{}}就表示成{1},再用2作为其ID。于是题目就成了编码问题,对每个新生成的集合,我们判断该集合是否出现过,若未出现过,就给他分配一个新的ID,出现过的用已有的ID表示。所有集合用map表示集合与ID的对应关系,用一个队列存集合,以便根据ID去集合。

于竞赛,这种题目还是不太可能碰到的、、于学习C++这门语言与掌握STL,这倒是个不错的题目。

对于那个置空,我们可以用C++中类的概念解释,写成if(op[0] == 'P') sk.push(getid(kong)); 或者因为我们知道他一定是1,所以也可以直接sk.push( 1 );

AC代码:(795ms)

#include<bits/stdc++.h>

using namespace std;
int top;
map<set<int>,int> s_i;
map<int,set<int> > i_s;
int getid(set<int> s) {
	if(s_i.find(s) != s_i.end()) return s_i[s];
	s_i[s] = ++top;
	i_s[top] = s;
	return top;
}

int main()
{
//	freopen("in.txt","r",stdin);
	int t,m;
	char op[10];
	set<int> kong,st1,st2,st;
	kong.clear();
	cin>>t;
	while(t--) {
		top = 0;
		s_i.clear();
		i_s.clear();
		stack<int> sk;
		scanf("%d",&m);
		while(m--) {
			
			scanf("%s",op);
			if(op[0] == 'P') sk.push(getid(kong)); 
			else if(op[0] == 'D') sk.push(sk.top());
			else {
				st1 = i_s[sk.top()];sk.pop();
				st2 = i_s[sk.top()];sk.pop();
				st.clear();//这一句必须加上,不然就wa
				if(op[0] == 'U') {
					set_union(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin()));
				}
				else if(op[0] == 'I') set_intersection(st1.begin(),st1.end(),st2.begin(),st2.end(),inserter(st,st.begin()));
				else {
					st2.insert(getid(st1));st = st2;
				}
				sk.push(getid(st));
			}
			printf("%d\n",i_s[sk.top()].size());
		}
		printf("***\n");
		
	}
	
	return 0 ;
}