zoukankan      html  css  js  c++  java
  • Uva 12096.The SetStack Computer

    嗯……这道题大思路很明显,但是细节好烦人……

    大体上就是stack+set

    对于栈中的元素,可以发现每个元素都是一个集合(set),而集合中的元素也是集合

    因此,应该对每个集合(元素)进行编号 typedef set<int> element ,这样就能把每个元素看作保存整数的栈 stack s; 

    用map和vector进行映射集合(元素)和编号

    map<element,int> ID_eache;
    vector<int> ID_eache2;

    然后再按照要求写就行了

    其中可用switch case判断操作。

    要注意每个case都需要 break; 

    因为case只是入口,并不是出口。只要进去后,没有break就会一直运行下去

     1 #include <cstdio>
     2 #include <set>
     3 #include <stack>
     4 #include <iostream>
     5 #include <map>
     6 #include <vector>
     7 using namespace std;
     8 
     9 class LOVE{
    10     private:
    11         int n;
    12         typedef set<int> element;
    13         stack <int> s;
    14 
    15         map<element,int> ID_cache;
    16         vector<element> ID_cache2;
    17 
    18         int ID(element x){
    19             if(ID_cache.count(x))return ID_cache[x];
    20             ID_cache2.push_back(x);
    21             return ID_cache[x]=ID_cache2.size()-1;
    22         }
    23 
    24     public:
    25         void start(){
    26             scanf("%d",&n);
    27             char com[10];
    28             while(n--){
    29                 scanf("%s",com);
    30                 element temp1,temp2,temp3;
    31                 element::iterator it1,it2,it;
    32                 switch(com[0]){
    33                     case 'P':
    34                         s.push(ID(element()));
    35                         break;
    36                     case 'D':
    37                         s.push(s.top());
    38                         break;
    39                     case 'U':
    40                         temp1=ID_cache2[s.top()];
    41                         s.pop();
    42                         temp2=ID_cache2[s.top()];
    43                         s.pop();
    44                         for(it=temp2.begin();it!=temp2.end();it++)
    45                             temp1.insert(*it);
    46                         s.push(ID(temp1));
    47                         break;
    48                     case 'I':
    49                         temp1=ID_cache2[s.top()];
    50                         s.pop();
    51                         temp2=ID_cache2[s.top()];
    52                         s.pop();
    53                         for(it1=temp1.begin();it1!=temp1.end();it1++){
    54                             for(it2=temp2.begin();it2!=temp2.end();it2++){
    55                                 if(*it1==*it2){
    56                                     temp3.insert(*it1);
    57                                     temp2.erase(*it2);
    58                                     break;
    59                                 }
    60                             }
    61                         }
    62                         s.push(ID(temp3));
    63                         break;
    64                     case 'A':
    65                         temp1=ID_cache2[s.top()];
    66                         s.pop();
    67                         temp2=ID_cache2[s.top()];
    68                         s.pop();
    69                         temp2.insert(ID(temp1));
    70                         s.push(ID(temp2));
    71                         break;
    72                 }
    73                 cout<<ID_cache2[s.top()].size()<<endl;
    74             }
    75         }
    76 };
    77 
    78 
    79 int main(){
    80     //freopen("in.txt","r",stdin);
    81     int n;
    82     scanf("%d",&n);
    83     while(n--){
    84         LOVE LIVE;
    85         LIVE.start();
    86         printf("***
    ");
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    kali禁止自动挂载U盘(gnome)
    Kali开启远程桌面服务(gnome桌面环境)
    KVM安装Win7时USB3.0无法使用的坑
    Linux上VLAN的创建
    小程序 局部页面 自定义滚动条
    两个图层一上一下div view
    js 数组去重
    css > 的写法 html
    块级元素和行内元素
    小程序 css 文字溢出,长度过长用 。。。
  • 原文地址:https://www.cnblogs.com/ohyee/p/5196129.html
Copyright © 2011-2022 走看看