• UVa 12096 The SetStack Computer【STL】


    题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数

    从紫书给的例子

    A={{},{{}}}

    B={{},{{{}}}}

    A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面也可以集合套集合

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring> 
     4 #include <cmath> 
     5 #include<stack>
     6 #include<vector>
     7 #include<map> 
     8 #include<set>
     9 #include<queue> 
    10 #include<algorithm>  
    11 #define mod=1e9+7;
    12 #define ALL(x) x.begin(),x.end()
    13 #define INS(x) inserter(x,x.begin())
    14 using namespace std;
    15 
    16 typedef long long LL;
    17 typedef set<int> Set;
    18 map<Set,int> IDcache;//每一个集合对应有一个ID编号 
    19 vector<Set> Setcache;//根据每个集合的ID编号找到对应的集合 
    20 
    21 
    22 
    23 int ID(Set x){
    24     if(IDcache.count(x)) return IDcache[x];//查找给定集合的编号,找到了,则返回编号 
    25     Setcache.push_back(x);//没有找到,则新建一个 
    26     return IDcache[x]=Setcache.size()-1;
    27 }
    28 
    29 int main(){
    30     int n,kase;
    31     cin>>kase;
    32     while(kase--){
    33         cin>>n;
    34         stack<int > s;
    35     for(int i=0;i<n;i++){
    36         string op;
    37         cin>>op;
    38         if(op[0]=='P') s.push(ID(Set()));
    39         else if(op[0]=='D') s.push(s.top());
    40         else{
    41             Set x1=Setcache[s.top()];s.pop();
    42             Set x2=Setcache[s.top()];s.pop();
    43             Set x;
    44             if(op[0]=='U') set_union(ALL(x1),ALL(x2),INS(x));//set_union求两个集合的并集 
    45             if(op[0]=='I') set_intersection(ALL(x1),ALL(x2),INS(x));//set_intersection求两个集合的交集 
    46             if(op[0]=='A') {x=x2;x.insert(ID(x1));}
    47             s.push(ID(x));
    48         }
    49         cout<<Setcache[s.top()].size()<<endl;
    50     }
    51     cout<<"***"<<"
    ";        
    52     }
    53     return 0;
    54 }
    View Code

    话说这一题目是之前看的了,但是当时始终不理解add操作

    ,后来看紫书的例子每一个栈里的元素是一个集合,集合又可以套集合,add操作就好理解一些了 就像是这幅插图一样

  • 相关阅读:
    (01)Hadoop简介
    (08)java程序连接kafka示例
    (02)使用 java -classpath 命令运行jar包脚本
    (01)Eclipse中导出jar包
    (07)Kafka核心配置详解
    (06)Kafka工作原理解析
    HDU
    HDU
    POJ3525:Most Distant Point from the Sea(二分+半平面交)
    POJ
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4358043.html
Copyright © 2020-2023  润新知