博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
12096:The SetStack Computer
阅读量:7281 次
发布时间:2019-06-30

本文共 1191 字,大约阅读时间需要 3 分钟。

为每个不同的集合分配一个唯一的ID,则每个集合都可以表示成所包含元素的ID集合,这样就可以用STL的set<int>来表示了,而整个栈则是一个stack<int>。

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define ALL(x) x.begin(),x.end()#define INS(x) inserter(x,x.begin())int T,n;typedef set
Set;map
IDcache; //将集合映射成IDvector
Setcache; //根据ID取集合int ID(Set x){ if(IDcache.count(x)) return IDcache[x]; Setcache.push_back(x); return IDcache[x] = Setcache.size() - 1;}int main(){    scanf("%d",&T);    while(T--){        char op[10]; IDcache.clear(); //其实不用clear也没关系 只是取个ID而已 对结果没影响 Setcache.clear(); //速度反而会更快 stack
s; cin>>n; while(n--){ scanf("%s",&op[0]); //cin读取大规模数据太慢 if(op[0] == 'P') s.push(ID(Set())); else if(op[0] == 'D') s.push(s.top()); else{ Set a = Setcache[s.top()];s.pop(); Set b = Setcache[s.top()];s.pop(); Set x; if(op[0] == 'U') set_union(ALL(a),ALL(b),INS(x)); if(op[0] == 'I') set_intersection(ALL(a),ALL(b),INS(x)); if(op[0] == 'A'){ x = b,x.insert(ID(a));} // 插入的是ID 不像上面是集合操作 s.push(ID(x)); } printf("%d\n",Setcache[s.top()].size()); } printf("***\n"); } return 0;}

转载于:https://www.cnblogs.com/JingwangLi/p/10202742.html

你可能感兴趣的文章
3分钟带你看懂android中的Binder机制
查看>>
说说vue-cli中使用flexible和px2rem-loader
查看>>
浅析 Vue 2.6 中的 nextTick 方法
查看>>
JavaScript链式调用实例
查看>>
webpack构建和性能优化探索
查看>>
区块链 | ETH投票项目
查看>>
simhash+汉明距离计算文本相似度
查看>>
mocha
查看>>
Node.js和NoSQL开发比特币加密货币应用程序(下)
查看>>
[LeetCode] 348. Design Tic-Tac-Toe
查看>>
移动端的适配及布局
查看>>
跨域访问
查看>>
aotoo-hub,一体式大前端架构
查看>>
Ubuntu创建新用户的正确姿势
查看>>
Webpack 4 教程 - 4. 使用SplitChunksPlugin插件进行代码分割
查看>>
项目经验不丰富、技术不突出的程序员怎么打动面试官?
查看>>
如何跨操作系统共享文件?你还在用U盘傻瓜式地拷贝文件吗?
查看>>
使用Jest测试JavaScript(Mock篇)
查看>>
J2EE 核心模式
查看>>
浅谈react性能优化的方法
查看>>