UVA12096 - The SetStack Computer(set + map映射)

UVA12096 - The SetStack Computer(set + map映射)

题目链接

题目大意:有五个动作:

push : 把一个空集合{}放到栈顶。

dup : 把栈顶的集合取出来,在入栈两次。

add : 出栈两次,把第一个集合作为一个元素放入第二个集合中,再将第二个集合入栈

union: 出栈两次,取这两个集合的并集,将结果入栈。

intersect: 出栈两次,取这两个集合的交集,将结果入栈。

每次执行动作后还需要输出目前栈顶集合的元素个数。

解题思路:这题可以用栈和set来模拟,push就把空的集合入栈,但是在并集和交集的时候就需要判段集合是否相同,所以这里可以用map把出现过的集合手动的映射成数字。

代码:

#include <cstdio>
#include <cstring>
#include <stack>
#include <set>
#include <map>

using namespace std;

char op[15];
int n;

typedef set<int> E;
stack<E> s;
map<E, int> vis;
E tmp1, tmp2;
set<int>::iterator it;
int num;

void hash (E a) {

    if (!vis.count(a))
        vis[a] = ++num;
}

void Push () {

    tmp1.clear();
    s.push (tmp1);
}

void Dup () {

    tmp1 = s.top();
    s.push (tmp1);
}

void Add () {

    tmp2 = s.top();
    s.pop();
    tmp1 = s.top();
    s.pop();
    tmp1.insert (vis[tmp2]);
    hash(tmp1);
    s.push(tmp1);
}

void Union () {

    tmp2 = s.top();
    s.pop();
    tmp1 = s.top();
    s.pop();
    for (it = tmp1.begin(); it != tmp1.end(); it++)
        tmp2.insert (*it);
    hash (tmp2);
    s.push (tmp2);
}

void Intersect () {

    tmp2 = s.top();
    s.pop();
    tmp1 = s.top();
    s.pop();
    E tmp;
    for (it = tmp1.begin(); it != tmp1.end(); it++)
        if (tmp2.count(*it))
            tmp.insert (*it);
    hash (tmp);
    s.push(tmp);
}

void solve () {

    switch (op[0]) {

        case ‘P‘ : Push();     break;
        case ‘D‘ : Dup();      break;
        case ‘U‘ : Union();    break;
        case ‘I‘ : Intersect(); break;
        case ‘A‘ : Add();      break;
    }
    printf ("%d\n", s.top().size());
}

void init () {

    num = 1;
    while (!s.empty()) {
        s.pop();
    }
    vis.clear();
}

int main () {

    int T;
    scanf ("%d", &T);
    while (T--) {

        scanf ("%d", &n);
        while (n--) {
            scanf ("%s", op);
            solve();
        }
        printf ("***\n");
    }
    return 0;
}

时间: 2024-10-07 08:41:16

UVA12096 - The SetStack Computer(set + map映射)的相关文章

UVa12096 - The SetStack Computer

The computer op erates on a single stack of sets, which is initially empty. After each op eration, the cardinality of the topmost set on the stack is output. The cardinality of a set S is denoted | S | and is thenumber of elements in S. The instructi

uva12096 The SetStack Computer By sixleaves

代码  1 #include <set> 2 #include <string> 3 #include <vector> 4 #include <map> 5 #include <stack> 6 #include <iostream> 7 #include <algorithm> 8 #define ALL(x) x.begin(), x.end() 9 #define INS(x) inserter(x, x.begi

12096 - The SetStack Computer

The SetStack Computer PS:因为该题排版较麻烦,这里给出OJ网址:UVa12096 - The SetStack Computer 有一个专门为了集合运算而设计的"集合栈"计算机.该机器有一个初始为空的栈,并且支持以下操作. PUSH:空集"{}"入栈. DUP:把当前栈顶元素复制一份后再入栈. UNION:出栈两个集合,然后把二者的并集入栈. INTERSECT:出栈两个集合,然后把二者的交集入栈. ADD:出栈两个集合,然后把先出栈的集合加

UVa 12096 The SetStack Computer

Description 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

UVA - 12096 The SetStack Computer(编码,STL)

12096 The SetStack Computer Background from Wikipedia: "Set theory is a branch of mathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century.Initially controversial, set theory has come to play the role o

UVAOJ 12096 The SetStack Computer(STL的运用)

12096 The SetStack Computer Background from Wikipedia: Set theory is a branch ofmathematics created principally by the German mathe-matician Georg Cantor at the end of the 19th century. Initially controversial, set theory has come to play therole of

12096 - The SetStack Computer UVA

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 m

uva 12096 - The SetStack Computer(STL)

UVA 12096 - The SetStack Computer 题目链接 题意:几个操作,push是在栈顶加入一个空集,dup是复制栈顶集合,在放入栈顶,union是把头两个取并集放回,int是头两个取交集放回,add是取头两个,把第一个当成一个集合加入第二个,每次操作输出栈顶集合的里面的个数 思路:用set,stack模拟,然后利用map去hash一个集合,模拟即可 代码: #include <cstdio> #include <cstring> #include <s

map——映射(message.cpp)

信息交换 (message.cpp) [题目描述] Byteland战火又起,农夫John派他的奶牛潜入敌国获取情报信息. Cow历尽千辛万苦终于将敌国的编码规则总结如下: 1 编码是由大写字母组成的字符串. 2 设定了密字.加密的过程就是把原信息的字母替换成对应密字. 3 一个字母有且仅有一个对应密字,不同字母对应不同密字. 如今,Cow终于获取了敌国发送的一条加密信息和对应的原信息.Cow如下破解密码:扫描原信息,对于原信息中的字母x,找到它在加密信息中的对应大写字母y,且认为y是x的密字.