集合栈计算机

 

#include<iostream>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;

#define ALL(x) x.begin(),x.end()//
#define INS(x) inserter(x,x.begin())

typedef set<int> Set;
map<Set, int> ID;//将每一个新的集合创建一个ID用于记录位置
vector<Set> idsearch;//通过map映射出的位置,找到该集合,每个集合都会存放在里面,id和search的下标一一对应

int idget(Set p)
{
    if (ID.count(p)) return ID[p];
    idsearch.push_back(p);
    return ID[p] = idsearch.size() - 1;
}

int main(void)
{
    int n;
    cin >> n;
    while (n--)
    {
        stack<int> s;//每个位置记录集合的id位置
        int cmd;
        cin >> cmd;
        ID.clear();
        idsearch.clear();
        for (int i = 0; i < cmd; i++)
        {
            string fin;
            cin >> fin;
            if (fin == "PUSH")  s.push(idget(Set()));
            else if (fin == "DUP") s.push(s.top());
            else
            {
                Set x1 = idsearch[s.top()]; s.pop();
                Set x2 = idsearch[s.top()]; s.pop();
                Set x3;
                //if (fin == "UNION")set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x3, x3.begin()));
                //if (fin == "INTERSECT")set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x3, x3.begin()));
                if (fin == "UNION")set_union(ALL(x1),ALL(x2),INS(x3));
                if (fin == "INTERSECT")set_intersection(ALL(x1), ALL(x2), INS(x3));
                if (fin == "ADD")
                {
                    x3 = x2;
                    x3.insert(idget(x1));
                }
                s.push(idget(x3));
            }
            cout << idsearch[s.top()].size() << endl;
        }
        cout << "***" << endl;
    }
    return 0;
}

 

posted @ 2021-01-30 12:16  loliconsk  阅读(76)  评论(0)    收藏  举报