UVa 12096 The SetStack Computer

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 mathemat-
ics concerning the existence of mathematical objects
(such as numbers or functions) and their properties.
Formal versions of set theory also have a founda-
tional 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 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 ≤ 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 <cstdio>
//#include <vector>
#include <set>
#include <stack>
#include <cstring>
#include <algorithm>
//#include <iostream>
using namespace std;
// 记录出现过的集合形式,下标为其编号
set<int> set_array[5000];
int array_count = 0;
// 建立一个栈来存放集合
set<int> set_stack[5000];
int stack_top = -1;

void push(set<int> x);
int find(set<int> x);


int main()
{
	int T, N;
	scanf("%d", &T);

	int t_count = 0;
	while(t_count < T)
	{
		// 记录空集为0
		memset(set_array, 0, sizeof(set_array));
		array_count = 1;
		memset(set_stack, 0, sizeof(set_stack));
		stack_top = -1;
		set_array[0] = set<int>();

		scanf("%d", &N);
		int n_count = 0;
		char str[10];
		while(n_count < N)
		{
			memset(str, 0, sizeof(str));
			scanf("%s", str);

			// 如果是PUSH
			if(strcmp(str, "PUSH") == 0)
			{
				// 得到一个空集
				push(set<int>());				
					
			}
			// 如果是DUP
			else if(strcmp(str, "DUP") == 0)
			{
				push(set_stack[stack_top]);	
			}
			// 如果是UNION
			else if(strcmp(str, "UNION") == 0)
			{
				set<int> set1 = set_stack[stack_top];
				set<int> set2 = set_stack[stack_top-1];	
				set<int> set3;
				set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set3, set3.begin()));
			
				// 如果set3是新元素,就插入到数组中
				if(!find(set3))
				{
					set_array[array_count] = set3;
					array_count++;
				}
				// 将set3设为栈顶
				stack_top--;
				set_stack[stack_top] = set3;				
			}
			// 如果是INTERSECT
			else if(strcmp(str, "INTERSECT") == 0)
			{
				set<int> set1 = set_stack[stack_top];
                                set<int> set2 = set_stack[stack_top-1];
                                set<int> set3;
                                set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set3, set3.begin()));

                                // 如果set3是新元素,就插入到数组中
                                if(!find(set3))
                                {
                                        set_array[array_count] = set3;
                                        array_count++;
                                }
                                // 将set3设为栈顶
                                stack_top--;
                                set_stack[stack_top] = set3;
			}
			// 如果是ADD
			else if(strcmp(str, "ADD") == 0)
			{
				set<int> set1 = set_stack[stack_top];
                                set<int> set2 = set_stack[stack_top-1];
				//set<int> set3 = set2;
				// 找到set1的编号
				int place = find(set1);
				// 插入到set2的集合中去
				set2.insert(place);

				// 如果set2是新集合,就插入数组
				if(!find(set2))
				{
					set_array[array_count] = set2;
                                        array_count++;
				}
				// 将set2设为栈顶
                                stack_top--;
                                set_stack[stack_top] = set2;		
			}

			// 打印栈顶元素集合的大小
			printf("%d\n", set_stack[stack_top].size());
			n_count++;
			
		}
		printf("***\n");
		t_count++;
	}	
	return 0;
}

// push操作
void push(set<int> x)
{
	stack_top++;
	set_stack[stack_top] = x;
} 

// 在集合数组中找该元素,如果找不到返回0,否则返回该元素的编号
int find(set<int> x)
{
	
	for(int i = 0; i < array_count; i++)
		if(set_array[i] == x)
			return i;
	return 0;
}


这道题没做出来,是一道好题目。问题核心在于如何表示可能会越来越复杂的集合。

因为所有集合都是从空集中构造出来,所以给每个新构造出来的集合一个编号,

然后集合的元素就变成了已构造集合的编号。这样集合必定是整数集合。


另外,set可以相互赋值,可以相互比较,set<T>()建立空集。

参考答案能加快速度的方法还可以使用map, 来建立集合及其编号之间的联系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值