The SetStack Computer id:12096

来源:互联网 发布:java可以生成apk吗 编辑:程序博客网 时间:2024/06/05 01:18
//题中要求的是集合的集合,所以用一个map将集合映射成一个int 类型的ID, 这样就便于编写代码//当然题中还需要用到栈,包含在头文件stack中//用vector来存储集合,stack来存储集合的id,而map主要用来将set转变成id#include <iostream>#include <cstdio>#include <string>#include <vector>#include <map>#include <stack>#include <set>#include <algorithm>using namespace std;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);    IDcache[x] = Setcache.size() - 1;    return IDcache[x];}int main(){     int T;     cin >> T;     while(T--) {        int n;        cin >> n;        stack<int> s;        for(int i = 0; i < n; i++) {            string cmd;            cin >> cmd;            if(cmd == "PUSH")       s.push(ID(Set()));            else if(cmd == "DUP")         s.push(s.top());            else {                Set x1 = Setcache[s.top()];     s.pop();                Set x2 = Setcache[s.top()];     s.pop();                Set x;                if(cmd == "UNION")  set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin()));                else if(cmd == "INTERSECT") set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin()));                else if(cmd == "ADD")   {x = x2;    x.insert(ID(x1));   }                s.push(ID(x));            }            cout << Setcache[s.top()].size() << endl;        }        cout << "***" << endl;     }    return 0;}

0 0
原创粉丝点击