UVa 12096 - The SetStack Computer

请看rujia大神的《算法竞赛入门经典 第2版》
P115~117

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <algorithm>

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x, x.begin())
using namespace std;
typedef set<int> myset;
map<myset, int> idcache;
vector<myset> setcache;
stack<int> s;
int get_id(myset x)
{
  if (idcache.count(x)) return idcache[x];
  setcache.push_back(x);
  return idcache[x] = setcache.size() - 1;
}
void solve()
{
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string op;
    cin >> op;
    if (op[0] == 'P') s.push(get_id(myset())); else
    if (op[0] == 'D') s.push(s.top()); else
    {
      myset x1 = setcache[s.top()];
      s.pop();
      myset x2 = setcache[s.top()];
      s.pop();
      myset x;
      if (op[0] == 'U') set_union(ALL(x1), ALL(x2), INS(x));
      if (op[0] == 'I') set_intersection(ALL(x1), ALL(x2), INS(x));
      if (op[0] == 'A')
      {
        x = x2;
        x.insert(get_id(x1));
      }
      s.push(get_id(x));
    }
    cout << setcache[s.top()].size() << endl;
  }
  puts("***");
}
int main()
{
// freopen("input.txt", "r", stdin);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++)
    solve();
}

你可能感兴趣的:(UVa 12096 - The SetStack Computer)