思路:分别定义栈,队列,优先队列(数值大的优先级越高)。每次放入的时候,就往分别向三个数据结构中加入这个数;每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结构,可以用三个标志变量来标志可能的数据结构。

注意:可能会出现数据结构为空的情况,要先判断非空才能取元素。


AC代码

  1. #include <stdio.h>
  2. #include <stack>
  3. #include <queue>
  4. using namespace std;
  5. //分别定义栈,队列,优先队列,然后模拟
  6. stack<int> sta;
  7. queue<int> que;
  8. priority_queue<int> p_que;
  9. void init() {
  10. while(!sta.empty()) sta.pop();
  11. while(!que.empty()) que.pop();
  12. while(!p_que.empty()) p_que.pop();
  13. }
  14. int main() {
  15. int n;
  16. while(scanf("%d", &n) == 1) {
  17. init();
  18. int tag1 = 1, tag2 = 1, tag3 = 1;
  19. for(int i = 0; i < n; ++i) {
  20. int tag, num;
  21. scanf("%d %d", &tag, &num);
  22. if (tag == 1) {
  23. sta.push(num);
  24. que.push(num);
  25. p_que.push(num);
  26. } else if(tag == 2) {
  27. if (sta.empty() || sta.top() != num) {
  28. tag1 = 0;
  29. } else sta.pop();
  30. if (que.empty() || que.front() != num) {
  31. tag2 = 0;
  32. } else que.pop();
  33. if (p_que.empty() || p_que.top() != num) {
  34. tag3 = 0;
  35. } else p_que.pop();
  36. }
  37. }
  38. int res = tag1 + tag2 + tag3;
  39. if (res == 0) {
  40. printf("impossible\n");
  41. } else if(res >= 2) {
  42. printf("not sure\n");
  43. } else {
  44. if (tag1) {
  45. printf("stack\n");
  46. } else if (tag2) {
  47. printf("queue\n");
  48. } else printf("priority queue\n");
  49. }
  50. }
  51. return 0;
  52. }

如有不当之处欢迎指出!

UVA - 11995 I Can Guess the Data Structure!(模拟)的更多相关文章

  1. [UVA] 11995 - I Can Guess the Data Structure! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

  2. UVa 11995:I Can Guess the Data Structure!(数据结构练习)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  3. UVA 11995 I Can Guess the Data Structure!(ADT)

    I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...

  4. UVA - 11995 - I Can Guess the Data Structure! STL 模拟

    There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...

  5. STL UVA 11995 I Can Guess the Data Structure!

    题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...

  6. UVa 11995 I Can Guess the Data Structure!

    做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...

  7. uva 11995 I Can Guess the Data Structure stack,queue,priority_queue

    题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...

  8. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)

    11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...

随机推荐

  1. OpenCv函数学习(一)

    Intel Image Processing Library (IPL) typedef struct _IplImage { int nSize; /* IplImage大小 */ int ID; ...

  2. java里程碑之泛型--深入理解泛型

    所谓泛型,就是允许在定义类,接口,方法时使用类型形参,这个类型形参将在声明变量,创建对象,调用方法的时候动态的指定.JAVA5之后修改了集合中所有的接口和类,为这些接口和类都提供了泛型的支持. 关于泛 ...

  3. 准备:新V8即将到来,Node.js的性能正在改变

    V8的Turbofan的性能特点将如何对我们优化的方式产生影响 审阅:来自V8团队的Franziska Hinkelmann和Benedikt Meurer. **更新:Node.js 8.3.0已经 ...

  4. 用户 'IIS APPPOOL\.NET v4.5 Classic' 登录失败。

    我在win8.1系统下用vs2013+SqlServer08编写完项目后,挪到另一台win8.1系统(安装了Vs2010+SqlServer08)中,把网址挂到IIs中时,出现如下错误 : 解决方案: ...

  5. 【django之orm小练习】

    作业1 创建单表Book表,要求字段: 1 主键 nid 2 书名 title 3 价格 price 4 出版日期 pubDate 5 出版社 publisher(普通字符串字段) class Boo ...

  6. iOS-Mac Charles抓包工具的使用【Mac 抓包工具Charles】

    1.下载文件 Charles安装包以及破解文件下载地址:http://charles.iiilab.com 2.安装及使用 使用介绍 http://www.cocoachina.com/ios/201 ...

  7. vsftpd安装和使用 Linux系统和window系统

    vsftpd 安装(Linux)一.安装系统环境 centos 6.9 64位二.vsftpd版本 vsftpd-2.2.2-24.el6.x86_64三.安装步骤1.安装 执行 yum -y ins ...

  8. RAID知识总结[转]

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://815632410.blog.51cto.com/1544685/1394306 ...

  9. BZOJ 2073: [POI2004]PRZ [DP 状压]

    传送门 水题不解释 这道题的主要目的在于记录一个枚举子集的技巧 #include <iostream> #include <cstdio> #include <cstri ...

  10. BZOJ 3262: 陌上花开 [CDQ分治 三维偏序]

    Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...