UVA12096 - The SetStack Computer(set + map映射)

题目链接

题目大意:有五个动作:

push : 把一个空集合{}放到栈顶。

dup : 把栈顶的集合取出来,在入栈两次。

add : 出栈两次。把第一个集合作为一个元素放入第二个集合中,再将第二个集合入栈

union: 出栈两次,取这两个集合的并集。将结果入栈。

intersect: 出栈两次。取这两个集合的交集,将结果入栈。

每次运行动作后还须要输出眼下栈顶集合的元素个数。

解题思路:这题能够用栈和set来模拟,push就把空的集合入栈,可是在并集和交集的时候就须要判段集合是否同样,所以这里能够用map把出现过的集合手动的映射成数字。

代码:

#include <cstdio>
#include <cstring>
#include <stack>
#include <set>
#include <map> using namespace std; char op[15];
int n; typedef set<int> E;
stack<E> s;
map<E, int> vis;
E tmp1, tmp2;
set<int>::iterator it;
int num; void hash (E a) { if (!vis.count(a))
vis[a] = ++num;
} void Push () { tmp1.clear();
s.push (tmp1);
} void Dup () { tmp1 = s.top();
s.push (tmp1);
} void Add () { tmp2 = s.top();
s.pop();
tmp1 = s.top();
s.pop();
tmp1.insert (vis[tmp2]);
hash(tmp1);
s.push(tmp1);
} void Union () { tmp2 = s.top();
s.pop();
tmp1 = s.top();
s.pop();
for (it = tmp1.begin(); it != tmp1.end(); it++)
tmp2.insert (*it);
hash (tmp2);
s.push (tmp2);
} void Intersect () { tmp2 = s.top();
s.pop();
tmp1 = s.top();
s.pop();
E tmp;
for (it = tmp1.begin(); it != tmp1.end(); it++)
if (tmp2.count(*it))
tmp.insert (*it);
hash (tmp);
s.push(tmp);
} void solve () { switch (op[0]) { case 'P' : Push(); break;
case 'D' : Dup(); break;
case 'U' : Union(); break;
case 'I' : Intersect(); break;
case 'A' : Add(); break;
}
printf ("%d\n", s.top().size());
} void init () { num = 1;
while (!s.empty()) {
s.pop();
}
vis.clear();
} int main () { int T;
scanf ("%d", &T);
while (T--) { scanf ("%d", &n);
while (n--) {
scanf ("%s", op);
solve();
}
printf ("***\n");
}
return 0;
}

最新文章

  1. MongoDB安装配置示例
  2. cordova for ios: Unable to simultaneously satisfy constraints.
  3. ahjesus Axure RP 7.0注册码
  4. Perl/Nagios – Can’t locate utils.pm in @INC
  5. How to Remove Table Partitioning in SQL Server
  6. 利用颜色生成UIImage
  7. Origin的图片导出问题
  8. struts2 注解方式
  9. configure mount nfs
  10. UPC 2959: Caoshen like math 这就是个水题
  11. 在一个页面重复使用一个js函数的方法
  12. 提升html5的性能体验系列之二列表流畅滑动
  13. dubbo的架构
  14. Django REST framework 中的序列化器
  15. [译] 理解 LSTM(Long Short-Term Memory, LSTM) 网络
  16. PHP字符串函数之 strcmp strncmp strcasecmp strncasecmp strnatcmp strnatcasecmp
  17. Java中BufferedReader到底是一个什么类?
  18. FreeSWITCH Git版本管理
  19. 【次小生成树】【Kruskal】【prim】【转】
  20. AutoCAD.net支持后台线程-Socket通讯

热门文章

  1. CentOS7重新生成 /boot/grub2/grub.cfg
  2. hihoCoder #1758 加减
  3. 求职之路(拿到百度、美团、趋势科技、华为offer)
  4. 糗事百科python爬虫
  5. COUNT多列,但是每列都是不同条件的,怎么用一句SQL写?
  6. sublime 常用快捷键备忘
  7. luogu 1969 积木大赛
  8. vim 搜尋取代功能
  9. react 使用antd的在图片列表或表格中实现点击其他元素Checkbox选中功能
  10. angular6安装