zoukankan      html  css  js  c++  java
  • UVa12096

    紫书例题

    题目链接

    https://vjudge.net/problem/UVA-12096

    AC 代码

    #include <iostream>
    #include <set>
    #include <map>
    #include <vector>
    #include <stack>
    #include <algorithm>
    
    using namespace std;
    
    typedef set<int> Set;
    map<Set, int> IDcache; // 把集合映射成 ID
    vector<Set> Setcache; // 根据 ID 取集合
    
    // 查找给定集合 x 的 ID。如果找不到,分配一个新 ID
    int ID(Set x)
    {
        if (IDcache.count(x)) return IDcache[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())
    
    set<int> s = set<int>();
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            stack<int> s;
            int n;
            cin >> n;
            for (int i = 0; i < n; i++)
            {
                string op;
                cin >> op;
                if (op[0] == 'P') s.push(ID(Set())); // Set() 表示一个空集,这里是把 ID 给 push 进去
                else if (op[0] == 'D') s.push(s.top());
                else
                {
                    Set x1 = Setcache[s.top()]; s.pop();
                    Set x2 = Setcache[s.top()]; s.pop();
                    Set x;
                    if (op[0] == 'U') set_union(ALL(x1), ALL(x2), INS(x)); // 这里要注意 inserter(x, x.begin) 的用法
                    if (op[0] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));
                    if (op[0] == 'A') { x = x2; x.insert(ID(x1)); }
                    s.push(ID(x));
                }
                cout << Setcache[s.top()].size() << endl;
            }
            cout << "***" << endl;
        }
    }
    

    按:这题主要是对 STL 的运用。其中,集合的交并操作是定义在 algorithm 头文件中。

  • 相关阅读:
    【Linux】命令——基本命令
    正则表达式
    Letex
    Markdown
    文本编辑器Vim
    【Linux】集群
    【Linux】软件安装
    共线性synteny
    windows触控手势
    【Linux】bin结尾的安装包
  • 原文地址:https://www.cnblogs.com/fanlumaster/p/14227112.html
Copyright © 2011-2022 走看看