您的位置:首页 > 其它

算法竞赛入门经典第五章例题5-5 The SetStack Computer UVA - 12096

2018-01-07 15:04 447 查看
https://vjudge.net/problem/UVA-12096

对每一个集合进行编号,那么任一个集合就是编号的集合,既可用
set<int>
类表示

#include<iostream>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<string>
using namespace std;
#pragma warning(disable:4996)
typedef set<int> Set;
map<Set, int> IDcache;
vector<Set> Setcache;
int ID(Set x) {
if (IDcache.count(x)) return IDcache[x];
Setcache.push_back(x);
return IDcache[x] = Setcache.size() - 1;
}
stack<int> s;
int T,N;
string str;
int main(){
#ifdef _DEBUG
freopen("in", "rb", stdin);
//freopen("out", "wb", stdout);
#endif // _DEBUG
cin >> T;
while (T--) {
cin >> N;
while (N--) {
cin >> str;
if(str[0]=='P') s.push(ID(Set()));
else if(str[0]=='D') s.push(s.top());
else{
Set x1 = Setcache[s.top()]; s.pop();
Set x2 = Setcache[s.top()]; s.pop();
if (str[0] == 'U')  x1.insert(x2.begin(), x2.end()), s.push(ID(x1));
else if (str[0] == 'I') {
auto it = x1.begin();
while (it != x1.end()) {
if (!x2.count(*it)) it = x1.erase(it);
else ++it;
}
s.push(ID(x1));
}
else x2.insert(ID(x1)),s.push(ID(x2));
}
cout << Setcache[s.top()].size() << endl;
}
cout << "***" << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: