POJ3121 HDU1968 UVA12096 UVALive3634 The SetStack Computer【set+vector+map+模拟】

The SetStack Computer
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1321   Accepted: 230

Description

POJ3121 HDU1968 UVA12096 UVALive3634 The SetStack Computer【set+vector+map+模拟】_第1张图片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 inmathematics concerning the existence of mathematical objects (such as numbers or functions) and their properties. Formal versions of set theory also have a foundational 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 Set-Stack 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 |S| 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 {} 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 first 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 = {{}, {{}}}

and that the next one is  
B = {{}, {{{}}}}.

For these sets, we have |A| = 2 and |B| = 2. Then:
  • UNION would result in the set { {}, {{}}, {{{}}} }. The output is 3.
  • INTERSECT would result in the set { {} }. The output is 1.
  • ADD would result in the set { {}, {{{}}}, {{},{{}}} }. The output is 3.

Input

An integer 0 ≤ T ≤ 5 on the first line gives the cardinality of the set of test cases. The first line of each test case contains the number of operations 0 ≤ N ≤ 2 000. Then follow N lines each containing one of the five 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 specified 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
***

Source

Northwestern Europe 2006

Regionals 2006 >> Europe - Northwestern


问题链接:POJ3121 HDU1968 UVA12096 UVALive3634 The SetStack Computer

问题简述:(略)

问题分析

  这个问题是模拟一个假想计算机的操作。

程序说明

  程序中使用了STL的多个容器类,也包含了多个容器类的运算操作,值得借鉴。

  这个程序在POJ中总是TLE,在HDU中一次侥幸AC(936ms,多数也是TLE)。使用STL是需要付出时间的代价的,也许使用简单的数据结构来表示,就不会出现TLE了。

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* POJ3121 HDU1968 UVA12096 UVALive3634 The SetStack Computer */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ALLSET(x) x.begin(), x.end()

using namespace std;

typedef set Set;
vector v;
map m;

int ID(Set x)
{
    if(m.count(x))
        return m[x];
    else {
        v.push_back(x);
        return m[x] = v.size() - 1;
    }
}

int main()
{
    int t, n;
    char op[10];

    scanf("%d", &t);
    while(t--) {
        stack s;

        scanf("%d", &n);
        for(int i=1; i<=n; i++) {
            scanf("%s", op);
            if(op[0] == 'P')    // PUSH
                s.push(ID(Set()));
            else if(op[0] == 'D')    // DUP
                s.push(s.top());
            else {
                Set x1 = v[s.top()]; s.pop();
                Set x2 = v[s.top()]; s.pop();
                Set x;
                if(op[0] == 'U')    // UNION
                    set_union(ALLSET(x1), ALLSET(x2), inserter(x, x.begin()));
                else if(op[0] == 'I')   // INTERSECT
                    set_intersection(ALLSET(x1), ALLSET(x2), inserter(x, x.begin()));
                else if(op[0] == 'A') {
                    x = x2;
                    x.insert(ID(x1));
                }
                s.push(ID(x));
            }
            printf("%d\n", (int)v[s.top()].size());
        }
        puts("***");
    }

    return 0;
}



你可能感兴趣的:(#,ICPC-备用二,#,ICPC-STL标准模板库,#,ICPC-POJ,#,ICPC-UVALive,#,ICPC-UVA,#,ICPC-HDU)