UVa 12096 The SetStack Computer 【STL】【stack】

来源:互联网 发布:布道者软件 编辑:程序博客网 时间:2024/05/22 12:29

题目链接:点击打开链接

紫书116页

AC代码:

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>#include <set>#include <map>#include <vector>#include <stack>using namespace std;#define ALL(x) x.begin(),x.end()#define INS(x) inserter(x, x.begin())typedef set<int> Set;map<Set,int> IDcache;vector<Set> Setcache;int ID(Set x){    if(IDcache.count(x)) return IDcache[x];    Setcache.push_back(x);    return IDcache[x] = Setcache.size() - 1;}int main(){    int t, n;    while(~scanf("%d",&t))    {        while(t--)        {            stack<int> s;            scanf("%d",&n);            while(n--)            {                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;}


几个用法:
s.empty()       //堆栈空为真
s.pop()           //移除栈顶元素
s.push()         //在栈顶增加元素
s.size()          //返回栈中元素数目
s.top()            //返回栈顶元素


0 0