The SetStack Computer(stack) -uva 12096

The SetStack Computer

Time Limit: 3000ms
Memory Limit: 131072KB
This problem will be judged on  UVA. Original ID:  12096
64-bit integer IO format:  %lld      Java class name:  Main


[PDF Link]

Problem B - The SetStack Computer

Time limit: 3 seconds

 Background from Wikipedia: Set 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 in mathematics 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 SetStack 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 PUSHDUPUNIONINTERSECT, 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 ≤ 2000. 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
***
 
    
#include
#include
#include
#include
#include
#include
using namespace std;

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())

typedef set Set;
map IDcache;			   //根据集合本身找其ID 
vector  Setcache;				//根据ID找其集合 


int ID (Set x)			//如果有此集合返回其ID,没有创建新ID 
{
	if(IDcache.count(x)) return IDcache[x];
	else
	{
		Setcache.push_back(x);
		IDcache[x]=Setcache.size()-1;
		return IDcache[x];	
	} 
}

vector printset(int id)  //根据ID显示集合 
{
	vector s;
	if(id==0)	
	{
		s.push_back('{');
		s.push_back('}');
		return s; 
	}
	else
	{
		s.push_back('{');
		for(Set::iterator it=Setcache[id].begin();it!=Setcache[id].end();it++)
		{
			vector s1(printset(*it));
			s.insert(s.end(),s1.begin(),s1.end());
			s.push_back(',');
		}
		s.pop_back();
		s.push_back('}');
		return s;
	}
	
	
} 

void print(vector& pile)
{
	for(vector::iterator it=pile.begin();it!=pile.end();it++)
	{
		vector s(printset(*it));
		for(int i=0;i  pile;            //可以用stack pile; 
	string s;
	int n;
	cin>>n;
	
	for(int i=0;i>s;
		
		if(s=="PUSH") 
		{
			pile.push_back(ID(Set()));
		}
		else if(s=="DUP")
		{
			pile.push_back(pile.back());	
		} 
		else
		{
		 	Set x1=Setcache[pile.back()];pile.pop_back();
		 	Set x2=Setcache[pile.back()];pile.pop_back();
		 	Set x;
		 	
		 	if(s=="UNION")  set_union(ALL(x1),ALL(x2),INS(x));
		 	if(s=="INTERSECT")  set_intersection(ALL(x1),ALL(x2),INS(x));
		 	if(s=="ADD")  {x=x2;x.insert(ID(x1));}
		 	pile.push_back(ID(x));
			
		}
		
//		cout<


你可能感兴趣的:(The SetStack Computer(stack) -uva 12096)