zoukankan      html  css  js  c++  java
  • 12096

    Background from Wikipedia: Set theory is a
    branch of mathematics created principally by the
    German mathematician Georg Cantor at the end of
    the 19th century. Initially controversial, set theory
    has come to play the role of a foundational theory
    in modern mathematics, in the sense of a theory
    invoked to justify assumptions made in mathemat-
    ics concerning the existence of mathematical objects
    (such as numbers or functions) and their properties.
    Formal versions of set theory also have a founda-
    tional role to play as specifying a theoretical ideal
    of mathematical rigor in proofs."
    Given this importance of sets, being the basis of mathematics, a set of eccentric theorist set off to
    construct a supercomputer operating on sets instead of numbers. The initial SetStack Alpha is under
    construction, and they need you to simulate it in order to verify the operation of the prototype.
    The computer operates on a single stack of sets, which is initially empty. After each operation, the
    cardinality of the topmost set on the stack is output. The cardinality of a set
    S
    is denoted
    j
    S
    j
    and is the
    number of elements in
    S
    . The instruction set of the SetStack Alpha is
    PUSH
    ,
    DUP
    ,
    UNION
    ,
    INTERSECT
    ,
    and
    ADD
    .
    
    PUSH
    will push the empty set
    fg
    on the stack.
    
    DUP
    will duplicate the topmost set (pop the stack, and then push that set on the stack twice).
    
    UNION
    will pop the stack twice and then push the union of the two sets on the stack.
    
    INTERSECT
    will pop the stack twice and then push the intersection of the two sets on the stack.
    
    ADD
    will pop the stack twice, add the rst set to the second one, and then push the resulting set
    on the stack.
    For illustration purposes, assume that the topmost element of the stack is
    A
    =
    ffg
    ;
    ffggg
    and that the next one is
    B
    =
    ffg
    ;
    fffgggg
    For these sets, we have
    j
    A
    j
    = 2 and
    j
    B
    j
    = 2. Then:
    
    UNION
    would result in the set
    ffg
    ,
    ffgg
    ,
    fffgggg
    . The output is 3.
    
    INTERSECT
    would result in the set
    ffgg
    . The output is 1.
    
    ADD
    would result in the set
    ffg
    ,
    fffggg
    ,
    ffg
    ,
    ffgggg
    . The output is 3.
    Input
    An integer 0
    
    T
    
    5 on the rst line gives the cardinality of the set of test cases. The rst line of each
    test case contains the number of operations 0
    
    N
    
    2000. Then follow
    N
    lines each containing one of
    the ve commands. It is guaranteed that the SetStack computer can execute all the commands in the
    sequence without ever popping an empty stack.
    Output
    For each operation specied in the input, there will be one line of output consisting of a single integer.
    This integer is the cardinality of the topmost element of the stack after the corresponding command
    has executed. After each test case there will be a line with `
    ***
    ' (three asterisks).
    SampleInput
    2
    9
    PUSH
    DUP
    ADD
    PUSH
    ADD
    DUP
    ADD
    DUP
    UNION
    5
    PUSH
    PUSH
    ADD
    PUSH
    INTERSECT
    SampleOutput
    0
    0
    1
    0
    1
    1
    2
    2
    2
    ***
    0
    0
    1
    0
    0
    ***
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <cmath>
      6 #include <string>
      7 #include <vector>
      8 #include <set>
      9 #include <map>
     10 #include <queue>
     11 #include <stack>
     12 #include <sstream>
     13 #include <cctype>
     14 using namespace std;
     15 const int INF = 0x7fffffff;
     16 const double EXP = 1e-8;
     17 const int MS = 105;
     18 typedef long long LL;
     19 int id;
     20 typedef set<int> SET;
     21 map<SET, int>  mp;
     22 typedef set<int>::iterator IT;
     23 stack<SET> sta;
     24 void ID(SET s)
     25 {
     26     if (mp.count(s))
     27         return;
     28     mp[s] = id++;
     29 }
     30 
     31 void PUSH()
     32 {
     33     SET S;
     34     ID(S);
     35     sta.push(S);
     36 }
     37 
     38 void DUP()
     39 {
     40     sta.push(sta.top());
     41 }
     42 
     43 void UNION()
     44 {
     45     SET S, S2;
     46     S2 = sta.top();
     47     sta.pop();
     48     S = sta.top();
     49     sta.pop();
     50     for (IT it = S2.begin(); it != S2.end(); it++)
     51         S.insert(*it);
     52     ID(S);
     53     sta.push(S);
     54 }
     55 
     56 void INTERSECT()
     57 {
     58     SET S, S2, S3;
     59     S2 = sta.top();
     60     sta.pop();
     61     S3 = sta.top();
     62     sta.pop();
     63     for (IT it = S2.begin(); it != S2.end(); it++)
     64     {
     65         if (S3.count(*it))
     66             S.insert(*it);
     67     }
     68     ID(S);
     69     sta.push(S);
     70 }
     71 
     72 void ADD()
     73 {
     74     SET S1, S2;
     75     S1 = sta.top();
     76     sta.pop();
     77     S2 = sta.top();
     78     sta.pop();
     79     S2.insert(mp[S1]);
     80     ID(S2);
     81     sta.push(S2);
     82 }
     83 
     84 void TOPSIZE()
     85 {
     86     cout << sta.top().size() << endl;
     87 }
     88 void solve()
     89 {
     90     char op[10];
     91     cin >> op;
     92     switch (op[0])
     93     {
     94     case 'P':PUSH(); break;
     95     case 'D':DUP(); break;
     96     case 'U':UNION(); break;
     97     case 'I':INTERSECT(); break;
     98     case 'A':ADD(); break;
     99     }
    100     TOPSIZE();
    101 }
    102 int main()
    103 {
    104     int T;
    105     cin >> T;
    106     while (T--)
    107     {
    108         int n;
    109         cin >> n;
    110         mp.clear();
    111         while (!sta.empty())
    112             sta.pop();
    113         id = 0;
    114         while (n--)
    115             solve();
    116         cout << "***" << endl;
    117     }
    118     return 0;
    119 }
  • 相关阅读:
    【C#】进行HTTP请求需要设置的参数
    【C#】Json转换Xml真的有那么麻烦吗?用两句话真正搞定Json转Xml
    N queens 2
    Combination sum
    Convert sorted array into binary search tree
    Climbing stairs LeetCode
    Convert sorted list into binary search tree
    N queens LeetCode
    解决IIS安装的问题:要求输入用户名和密码
    实现动态下拉框
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4246654.html
Copyright © 2011-2022 走看看