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

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3248

    13916058 12096 The SetStack Computer Accepted C++ 0.302 2014-07-21 03:43:15

    The SetStack Computer

    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 unde
    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 jSj 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 se
    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 jAj = 2 and jBj = 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 o
    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).
    Sample Input
    2
    9
    PUSH
    DUP
    ADD
    PUSH
    ADD
    DUP
    ADD
    DUP
    UNION
    5
    PUSH
    PUSH
    ADD
    PUSH
    INTERSECT
    Sample Output
    0
    0
    1
    0
    1
    1
    2
    2
    2
    ***
    0
    0
    1
    0
    0
    ***


     解题思路:模拟五个操作即可。读题十分重要。还有通过此题,可以增加使用set容器的熟练程度。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <stack>
     5 #include <map>
     6 #include <set>
     7 using namespace std;
     8 const int MAXN = 2010;
     9 const int N = 20;
    10 
    11 int cnt;
    12 stack<set<int> > stk;
    13 map<set<int>, int> mp;
    14 set<int> s1, s2;
    15 
    16 void pop() {
    17     s1 = stk.top();
    18     stk.pop();
    19     s2 = stk.top();
    20     stk.pop();
    21 }
    22 
    23 void Push() {
    24     set<int> s;
    25     stk.push(s);
    26     printf("0
    ");
    27 }
    28 
    29 void Dup() {
    30     set<int> s;
    31     s = stk.top();
    32     stk.push(s);
    33     printf("%d
    ", s.size());
    34 }
    35 
    36 void Union() {
    37     pop();
    38     set<int>::iterator it;
    39     for (it = s1.begin(); it != s1.end(); it++)
    40         s2.insert(*it);
    41     stk.push(s2);
    42     printf("%d
    ", s2.size());
    43 }
    44 
    45 void Intersect() {
    46     pop();
    47     set<int> s3;
    48     set<int>::iterator it;
    49     for (it = s1.begin(); it != s1.end(); it++)
    50         if (s2.find(*it) != s2.end())
    51             s3.insert(*it);
    52     stk.push(s3);
    53     printf("%d
    ", s3.size());
    54 }
    55 
    56 void Add() {
    57     pop();
    58     if (s1.empty())
    59         s2.insert(0);
    60     else {
    61         if (!mp[s1])
    62             mp[s1] = cnt++;
    63         s2.insert(mp[s1]);
    64     }
    65     stk.push(s2);
    66     printf("%d
    ", s2.size());
    67 }
    68 
    69 int main() {
    70     int t, n;
    71     string str;
    72     cin >> t;
    73     while ( t --) {
    74         cin >> n;
    75         while (!stk.empty())
    76             stk.pop();
    77         cnt = MAXN;
    78         mp.clear();
    79         while (n--) {
    80             cin >> str;
    81             if (str[0] == 'P')
    82                 Push();
    83             else if (str[0] == 'D')
    84                 Dup();
    85             else if (str[0] == 'U')
    86                 Union();
    87             else if (str[0] == 'I')
    88                 Intersect();
    89             else Add();
    90         }
    91         printf("***
    ");
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    深度学习:卷积神经网络(convolution neural network)
    Caffe使用step by step:r-cnn目标检测代码
    Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
    Caffe使用step by step:caffe框架下的基本操作和分析
    (论文阅读)2015.10.8图像识别中的深度学习
    Caffe搭建:常见问题解决办法和ubuntu使用中遇到问题(持续更新)
    Ubuntu 14.04(64bit)使用indicator-sysmonitor显示系统运行状态
    SpringBoot配置文件自动映射到属性和实体类(8)
    SpringBoot热部署(7)
    SpringBoot之MultipartFile文件上传(6)
  • 原文地址:https://www.cnblogs.com/Destiny-Gem/p/3858134.html
Copyright © 2011-2022 走看看