uva 12096 - The SetStack Computer(STL 的运用)

这道题目貌似就是在不停地用STL中的内容,对STL熟练运用的大神估计坐起来会比较easy。。

不过对于我这种看着代码还是需要上网查函数运用的菜鸟来说,若让我自己做这道题,肯定不会使用STL。。

就当对STL的学习了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<algorithm>//set_union函数在这个头文件里面;
using namespace std;//set vector等需要使用命名空间才能使用;
typedef set<int> Set;
map<Set,int> IDcache;
vector<Set> Setcache;

int ID(Set x)
{
    if(IDcache.count(x)) return IDcache[x];//map.count(r)是查找元素x出现的次数;
    Setcache.push_back(x);
    return IDcache[x]=Setcache.size()-1;
}

#define ALL(x) x.begin(),x.end() // 表示 所有内容
#define INS(x) inserter(x,x.begin()) //表示 插入迭代器

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        stack<int> s;
        int n;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            string op;
            cin>>op;
            if(op=="PUSH")
            {
                s.push(ID(Set()));
            }
            else if(op=="DUP") s.push(s.top());
            else
            {
                Set x1=Setcache[s.top()];
                s.pop();
                Set x2=Setcache[s.top()];
                s.pop();
                Set x;
                if(op=="UNION") set_union (x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(op=="INTERSECT") set_intersection (x1.begin(),x1.end(),x2.begin(),x2.end(),inserter(x,x.begin()));
                if(op=="ADD")
                {
                    x=x2;
                    x.insert(ID(x1));
                }
                s.push(ID(x));
            }
            cout<<Setcache[s.top()].size()<<endl;
        }
        printf("***\n");
    }
    return 0;
}
时间: 2024-12-19 13:42:50

uva 12096 - The SetStack Computer(STL 的运用)的相关文章

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

UVa - 12096 The SetStack Computer(STL容器综合,强推!)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=42064 #include <iostream> #include <algorithm> #include <string> #include <map> #include <set> #include <vector> #include <stack> #define ALL(x) x.b

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

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 set)

题意: 有5种操作: PUSH:加入"{}"空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出栈的加入到后出栈的集合中. 输入不超过2000, 保证操作顺利进行. 分析: 用set<int>(本身又可以映射成一个int)去模拟集合,所以可以将不同的集合映射成int型. 用一个Map<set<int>,int> 去映射成不同的int. 以后需要se

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

12096 - The SetStack Computer

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

UVA 12096 STL map set 的使用

set这个容器也是STL库的一员,并且在algorithm内直接有 set_union set_intersection  这样求并集交集的操作 map 最方便的地方就是 支持下标访问 举例说明 : 1 #include<iostream> 2 include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<map> 6 #include<set> 7 #includ