uva12096集合栈计算机

背景:这道题出现在我学习c++的stl中,对现在还不了解stl的人来说,确实显得太难了,只有照着书打下代码,然后一步一步的理解。

思路:由于本题的的集合并不是简单的整数集合或者字符串集合,所以就用map建立映射关系,而再建立映射关系的时候,又运用了vector协助,这样就能很明确的使得每个集合都有一个ID,然后就是堆栈的一些操作了。

学习:stl里面一些知识的简单运用。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef set Set;
map IDcache;
vector Setcache;
int ID(Set x)
{
    if(IDcache.count(x)) return IDcache[x];
    Setcache.push_back(x);
    return IDcache[x]=Setcache.size()-1;
}
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
stack s;
int main(void)
{
    int n;
    cin>>n;
    while(n--)
    {
        int m;
        cin>>m;
        for(int i=0;i>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<


你可能感兴趣的:(uva)