UVa 12096 - The SetStack Computer

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x, x.begin())
using namespace std;
typedef set myset;
map idcache;
vector setcache;
stack 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)