/*************************************************************************
> File Name: 12096.cpp
> Author: gwq
> Mail: [email protected]
> Created Time: 2015年01月21日 星期三 15時14分14秒
************************************************************************/
#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <iterator>
#include <algorithm>
#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())
using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
const double esp = 1e-5;
typedef set<int> Set;
map<Set, int> id; // 把集合映射成id
vector<Set> vset; // 根據id取集合
// 查找給定集合的id,如果沒有就分配一個
int get_id(Set s)
{
if (id.count(s)) {
return id[s];
} else {
vset.pb(s); // 添加新集合
return id[s] = vset.size() - 1;
}
}
int main(int argc, char *argv[])
{
int t;
scanf("%d", &t);
while (t--) {
int n;
stack<int> s;
cin >> n;
for (int i = 0; i < n; ++i) {
string op;
cin >> op;
if (op[0] == 'P') {
s.push(get_id(Set()));
} else if (op[0] == 'D') {
s.push(s.top());
} else {
Set x1 = vset[s.top()];
s.pop();
Set x2 = vset[s.top()];
s.pop();
Set x;
if (op[0] == 'U') {
set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin()));
} else if (op[0] == 'I') {
set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin()));
} else if (op[0] == 'A') {
x = x2;
x.insert(get_id(x1));
}
s.push(get_id(x));
}
cout << vset[s.top()].size() << endl;
}
cout << "***" << endl;
}
return 0;
}
uvaoj 12096 The SetStack Computer 集合操作用法
uvaoj 12096 The SetStack Computer 集合操作用法
有一個初始爲空的棧,有好多類型的操作,PUSH,DUP,UNION,INTERSECT,ADD,因爲是集合的集合,所以使用STL中的set,和對set的操作函數,set_union和set_intersection,和函數inserter。
代碼如下:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.