UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)

题意分析

绝对的好题。

先说做完此题的收获:

1.对数据结构又有了宏观的上的认识;

2.熟悉了常用STL(set,map,vector)的常用用法;

3.学习了一种问题转化的方式。

我想如果告诉我这题用STL解决,并且还能独立完成的话,那么STL应该算是过关了。

有下列操作集:

1.PUSH:向栈顶PUSH一个空集合的元素;

2.DUP: 弹出栈顶元素;

3.UNION: 弹出2个栈顶元素,并且去并集后入栈;

4.INTERSECT:弹出2个栈顶元素,去交集后入栈;

5.ADD:弹出2个栈顶元素,将第一个元素表示的集合加入到第二个元素中。

每次执行完操作,要输出当前栈顶元素中的集合个数,若为空集,则输出0。

我的第一感觉是set类型的stack,但是由于对STL这方面的不熟悉,没有着手实现,而是直接参考了lrj大大的写法。

未完:有时间一定补上题解!!

代码总览

  1. #include <iostream>
  2. #include <set>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <map>
  7. #include <stack>
  8. #include <vector>
  9. using namespace std;
  10. typedef map<set<int>,int> Map;
  11. typedef set<int> Set;
  12. vector<Set> IDstore;
  13. Map IDmap;
  14. int findid(Set t)
  15. {
  16. if(IDmap.count(t) == 1) return IDmap[t];
  17. IDstore.push_back(t);
  18. return IDmap[t] = IDstore.size() -1;
  19. }
  20. int main()
  21. {
  22. //freopen("in.txt","r",stdin);
  23. int t;
  24. cin>>t;
  25. while(t--){
  26. stack<int> s;
  27. int n;
  28. string ord;
  29. cin>>n;
  30. while(n--){
  31. cin>>ord;
  32. if(ord[0] == 'P'){
  33. s.push(findid(Set()));
  34. }else if(ord[0] == 'D'){
  35. s.push(s.top());
  36. }else{
  37. Set t1 = IDstore[s.top()];s.pop();
  38. Set t2 = IDstore[s.top()];s.pop();
  39. Set t;
  40. if(ord[0] == 'U'){
  41. set_union(t1.begin(),t1.end(),t2.begin(),t2.end(),inserter(t,t.begin()));
  42. }else if(ord[0] == 'I'){
  43. set_intersection(t1.begin(),t1.end(),t2.begin(),t2.end(),inserter(t,t.begin()));
  44. }else if(ord[0] == 'A'){
  45. t = t2;
  46. t.insert(findid(t1));
  47. }
  48. s.push(findid(t));
  49. }
  50. cout<<IDstore[s.top()].size()<<endl;
  51. }
  52. cout<<"***"<<endl;
  53. }
  54. return 0;
  55. }

UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)的更多相关文章

  1. uva 12096 - The SetStack Computer(集合栈)

    例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC 2006,UVa12096) 有一个专门为了集合运算而设计的"集合栈"计算机. ...

  2. uva 12096 The SetStack Computer

    点击打开链接uva 12096 思路: STL模拟 分析: 1 题目给定5种操作,每次输出栈顶集合的元素的个数 2 利用stack和set来模拟,set保存集合的元素.遇到push的时候直接在stac ...

  3. UVa 12096 The SetStack Computer【STL】

    题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...

  4. uva 12096 The SetStack Computer(STL set的各种库函数 交集 并集 插入迭代器)

    题意: 有5种操作: PUSH:加入“{}”空集合入栈. DUP:栈顶元素再入栈. UNION:出栈两个集合,取并集入栈. INTERSECT:出栈两个集合,取交集入栈. ADD:出栈两个集合,将先出 ...

  5. 12096 - The SetStack Computer UVA

    Background from Wikipedia: \Set theory is a branch of mathematics created principally by the German ...

  6. UVa12096.The SetStack Computer

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096)

    集合栈计算机(The SetStack Computer, ACM/ICPC NWERC 2006,Uva12096) 题目描述 有一个专门为了集合运算而设计的"集合栈"计算机.该 ...

  8. EOJ 1641/UVa The SetStack Computer

    Background from Wikipedia: “Set theory is a branch of mathematics created principally by the German ...

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

    UVA12096 - The SetStack Computer(set + map映射) 题目链接 题目大意:有五个动作: push : 把一个空集合{}放到栈顶. dup : 把栈顶的集合取出来, ...

随机推荐

  1. WeTest功能优化第2期:云真机智能投屏,调试告别鼠标

    第2期功能优化目录 [云真机视频映射]云真机画面本地映射[兼容性测试报告]新增问题机型聚类功能[新增Android9.0]同步上线最新安卓系统 本期介绍的云测产品功能优化,既有重磅级技术突破,也有报告 ...

  2. hdu1730Northcott Game(nim博弈)

    Northcott Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. Qt listwigwt item 加入自定义元素

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

  4. Python3.5+selenium(11)脚本模块化&参数化

    mail126.py脚本如下 from selenium import webdriver from time import sleep from model1 import Login driver ...

  5. 【WXS数据类型】String

    属性: 名称 值类型 说明 [String].constructor [String] 返回值为“String”,表示类型的结构字符串 [String].length [Number] 返回该字符串的 ...

  6. 简单说明hadoop集群运行三种模式和配置文件

    Hadoop的运行模式分为3种:本地运行模式,伪分布运行模式,集群运行模式,相应概念如下: 1.独立模式即本地运行模式(standalone或local mode)无需运行任何守护进程(daemon) ...

  7. 【第四章】Shell 条件测试表达式

    shell中条件测试的三种格式: 格式1: test 条件表达式格式2: [ 条件表达式 ]格式3: [[ 条件表达式 ]] 使用test: [root@host- ~]# test -f file ...

  8. 局部加权回归(LWR) Matlab模板

    将百度文库上一份局部加权回归的代码,将其改为模板以便复用. q2x,q2y为数据集,是n*1的矩阵: r是波长参数,就是对于距离的惩罚力度: q_x是要拟合的数据横坐标,是1*n的矩阵: 得到的q_y ...

  9. POJ 2826 An Easy Problem?!(线段交点+简单计算)

    Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Be ...

  10. svn升级(mac)

    原文链接:http://www.jianshu.com/p/c81712ecccb8 升级前 svn版本1.7.20 升级之后 1.9.2 步骤: 1. 下载最新版svn,链接:http://www. ...