UVA12096 the SetStack Computer(STL中,map,set,vector,stack联合应用)

来源:互联网 发布:美工的职业目标 编辑:程序博客网 时间:2024/05/18 00:43

题目连接:

传送门

#include<stdio.h>#include<iostream>#include<algorithm>#include<map>#include<vector>#include<set>#include<stack>#define ALL(x) x.begin(),x.end()#define INS(x) inserter(x,x.begin())using namespace std;//把set定义成类型typedef set<int> Set;//map的作用是,不同的集合,都映射一个值map<Set,int> mmp;//vector是对于给出的map的值,能在这里找到对应的setvector<Set> vec;//stack 模拟题目中的栈,而里面存的是int型,它的下标可根据vector里面的set,可以找到对应其的集合,map在其中起连接作用stack<int>sta;// isset函数的作用是判断集合x是否在map中,如果在,就返回他多对应的值,如果不在,加入vector中,并在map中建立相应的映射关系int isset(Set x){    if(mmp.count(x)) return mmp[x];    vec.push_back(x);    return mmp[x]=vec.size()-1;}int  main(){    int T;    cin>>T;    while(T--){    //初始化        mmp.clear();        while(!sta.empty())sta.pop();        vec.clear();        int n;        cin>>n;        for(int i=0;i<n;i++)        {            string str;            cin>>str;            if(str=="PUSH")sta.push(isset(Set()));            else if(str=="DUP") sta.push(sta.top());            else            {                Set x1=vec[sta.top()];sta.pop();                Set x2=vec[sta.top()];sta.pop();                Set x;                if(str=="UNION") set_union(ALL(x1),ALL(x2),INS(x));                if(str=="INTERSECT") set_intersection(ALL(x1), ALL(x2), INS(x));                if(str=="ADD") {                    x=x2;x.insert(isset(x1));                }                sta.push(isset(x));            }            cout<<vec[sta.top()].size()<<endl;        }            cout<<"***"<<endl;    }    return 0;}
阅读全文
0 0
原创粉丝点击