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 头文件中。

  • 相关阅读:
    HTML5
    PHP
    eclipse项目导入到android studio
    Jpush教材
    Android性能优化典范
    Fresco好案例
    扫二维码关注微信号,回复“送礼包”就送超值大礼!
    Android开源项目大全之工具库
    android学习“知乎”建议
    C# Json时间类型的转换
  • 原文地址:https://www.cnblogs.com/fanlumaster/p/14227112.html
Copyright © 2011-2022 走看看