The SetStack Computer——Uva 12096

Background from Wikipedia: “Set theory is abranch of mathematics created principally by theGerman mathematician Georg Cantor at the end ofthe 19th century. Initially controversial, set theoryhas come to play the role of a foundational theoryin modern mathematics, in the sense of a theoryinvoked 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 idealof mathematical rigor in proofs.”

Given this importance of sets, being the basis of mathematics, a set of eccentric theorist set off toconstruct a supercomputer operating on sets instead of numbers. The initial SetStack Alpha is underconstruction, 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, thecardinality of the topmost set on the stack is output. The cardinality of a set S is denoted |S| and is thenumber 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 seton 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 eachtest case contains the number of operations 0 N 2000. Then follow N lines each containing one ofthe five commands. It is guaranteed that the SetStack computer can execute all the commands in thesequence 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 commandhas 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
*** 

本题分析:

    首先这道题我觉得超奇怪,因为它给的是集合的集合,不是数的集合或者是字母的集合。而且我还有不明白的地方就是这道题并没有给出集合是啥东西,太抽象了的感觉......

    书上是这样写的:为了方便起见,此处为每个不同的集合分配一个唯一的ID,那么每个集合都可以表示成为所包含元素的ID集合,那么这样就可以用STL的set来表示了,而整个栈则是一个stack


书上代码:

  

#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取集合

//查找给定集合x的ID。如果找不到,分配一个新的ID
int ID (Set x) 
{
    if(IDcache.count(x))
        return IDcache[x];
    Setcache.push_back(x);//添加新的集合
    return IDcache[x] = Setcache.size() - 1;
}

int main(){
    stack s;//题目中的栈
    int n, t;
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            string op;
            cin >> op;
            if(op[0] == 'P')
                s.push(ID(Set()));
            else if(op[0] == 'D')
                s.push(s.top());
            else 
            {
                Set x1 = Setcache[s.top()];
                s.pop();
                Set x2 = Setcache[s.top()];
                s.pop();
                Set x;
                if(op[0] == 'U')
                set_union(ALL(x1), ALL(x2), INS(x));
                if(op[0] == 'I')
                    set_intersection(ALL(x1), ALL(x2), INS(x));
                if(op[0] == 'A')
                {
                    x = x2;
                    x.insert(ID(x1));
                }
                s.push(ID(x));
            }
            cout << Setcache[s.top()].size() << endl;
        }
        cout << "***" << endl;
    }
	return 0;
}

补充知识点——关于set_union和set_intersection

  • set_union和set_intersection都是头文件下的。也就是题目中的并集和交集。
  • std::set_union

    template 
      OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2,
                                OutputIterator result);
    以上是默认情况下的格式。
    常用格式是:
  • template 
      OutputIterator set_union (InputIterator1 first1, InputIterator1 last1,
                                InputIterator2 first2, InputIterator2 last2,
                                OutputIterator result, Compare comp);
    The  union  of two sets is formed by the elements that are present in either one of the sets, or in both. Elements from the second range that have an equivalent element in the first range are not copied to the resulting range.

    std::set_intersection

    template 
      OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                                       InputIterator2 first2, InputIterator2 last2,
                                       OutputIterator result);
    以上是默认情况下的格式。
  • 常用格式是:
  • template 
      OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                                       InputIterator2 first2, InputIterator2 last2,
                                       OutputIterator result, Compare comp);
    The intersection of two sets is formed only by the elements that are present in both sets. The elements copied by the function come always from the first range, in the same order.


你可能感兴趣的:(算法竞赛入门经典例题)