UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer

Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.

Initially controversial, set theory has come to play therole of a foundational theory in modern mathematics,
in the sense of a theory invoked to justify assumptionsmade in mathematics concerning the existence of mathe-
matical objects (such as numbers or functions) and theirproperties. Formal versions of set theory also have a
foundational role to play as specifying a theoretical idealof mathematical rigor in proofs."
Given this importance of sets, being the basis ofmathematics, a set of eccentric theorist set off to construct a supercomputer operating on sets in-stead 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 jSj 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 fg 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 rst set to the second one, and then push the resulting seton the stack.

Input
An integer 0 <= T<= 5 on the rst line gives the cardinality of the set of test cases. The rst line of each
test case contains the number of operations 0 <=N <= 2000. Then follow N lines each containing one of
the ve 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 speci ed 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
***

题目连接http://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&category=242&page=show_problem&problem=3248

维护  N(1N2000)  个操作, 可能的五种操作如下:
PUSH: 在栈顶加入一个空集合  A={}  ;
DUP: 把栈顶集合  A  复制一遍再加进去;
UNION: 弹出栈顶集合  A  及其下集合  B  , 把它们的并集加入栈;
INTERSECT: 弹出栈顶集合  A  及其下集合  B  , 把他们的交集加入栈;
ADD: 弹出栈顶集合  A  及其下集合  B  , 把栈顶集合  A  作为一个元素加入集合  B  , 再把集合  B  加入栈中.

对于每个操作, 输出栈顶集合  A  中的元素个数.

用set来模拟集合,对不同的集合进行序号化,然后把序号化的数字压入栈中,取出再由序号找到对应的集合输出栈顶集合元素个数即可,需要一定的对stl运用的能力

#include<iostream>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
map<set<int>,int> IDcache;  //对不同集合进行序号化 
vector<set<int > > Setcache;  //根据序号取出集合 
int ID(set<int> x)
{
    if(IDcache.count(x)) return IDcache[x];
    else {
        Setcache.push_back(x);
        return IDcache[x]=Setcache.size()-1;
    }
}
int main()
{
    int cases,n;
    cin>>cases;
    while(cases--)
    {
        cin>>n;
        stack<int>s;
        while(!s.empty()) s.pop();        //对stack做清空初始化。
        IDcache.clear();
        Setcache.clear();
        for(int i=0;i<n;i++)
        {
            string op;
            set<int> x1,x2,x;
            cin>>op;
            if(op[0]=='P') x=set<int>();      //或者写成“if(op[0]=='P');”就是让x是空的集合即可,元素是空即可。
            if(op[0]=='D') {
                x=Setcache[s.top()];
            }
            if(op[0]=='U'){
                x1=Setcache[s.top()]; s.pop();
                x2=Setcache[s.top()]; s.pop();
                set_union(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin())); //取并集函数 
            }
            if(op[0]=='I'){
                x1=Setcache[s.top()]; s.pop();
                x2=Setcache[s.top()]; s.pop();
                set_intersection(x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin())); //取交集函数 
            }
            if(op[0]=='A'){
                x1=Setcache[s.top()]; s.pop();
                x2=Setcache[s.top()]; s.pop();
                x =x2;
                x.insert(IDcache[x1]);
            }
            s.push(ID(x));
            cout<<Setcache[s.top()].size()<<endl;
        }
        cout<<"***\n";

    }
    return 0;
}

以上过程中用到取交集并集的函数,函数的运用知识见 STL 算法vector/set集合-交集,并集,差集,对称差


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值