I Can Guess the Data Structure!

There is a bag-like data structure, supporting two operations:

1 x1 x: Throw an element xx into the bag.

22: Take out an element from the bag.

Given a sequence of operations with return values, you’re going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!

Input

There are several test cases. Each test case begins with a line containing a single integer nn (1≤n≤10001≤n≤1000). Each of the next nn lines is either a type-1 command, or an integer 22 followed by an integer xx. This means that executing the type-2 command returned the element xx. The value of xx is always a positive integer not larger than 100100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.

Output

For each test case, output one of the following:

stack
It’s definitely a stack.

queue
It’s definitely a queue.

priority queue
It’s definitely a priority queue.

impossible
It can’t be a stack, a queue or a priority queue.

not sure
It can be more than one of the three data structures mentioned above.

Sample Input 1 Sample Output 1
  1. 6
  2. 1 1
  3. 1 2
  4. 1 3
  5. 2 1
  6. 2 2
  7. 2 3
  8. 6
  9. 1 1
  10. 1 2
  11. 1 3
  12. 2 3
  13. 2 2
  14. 2 1
  15. 2
  16. 1 1
  17. 2 2
  18. 4
  19. 1 2
  20. 1 1
  21. 2 1
  22. 2 2
  23. 7
  24. 1 2
  25. 1 5
  26. 1 1
  27. 1 3
  28. 2 5
  29. 1 4
  30. 2 4
  31. 1
  32. 2 1
  1. queue
  2. not sure
  3. impossible
  4. stack
  5. priority queue
  6. impossible

题意

模拟栈stack,队列queue,和优先队列priority queue的进出,判断属于哪一个

思路

用stl模拟

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int n, a, x;
  5. while(cin >> n) {
  6. stack<int> s;
  7. queue<int> q;
  8. priority_queue<int> pq;
  9. bool iss = , isq = , ispq = ;
  10. while(n--) {
  11. cin >> a >> x;
  12. if(a == ) {
  13. s.push(x);
  14. q.push(x);
  15. pq.push(x);
  16. } else {
  17. if(s.empty() || s.top() != x) iss = ;
  18. if(q.empty() || q.front() != x) isq = ;
  19. if(pq.empty() || pq.top() != x) ispq = ;
  20. if(!s.empty()) s.pop();
  21. if(!q.empty()) q.pop();
  22. if(!pq.empty()) pq.pop();
  23. }
  24. }
  25. if(iss + isq + ispq > )
  26. puts("not sure");
  27. else if(iss)
  28. puts("stack");
  29. else if(isq)
  30. puts("queue");
  31. else if(ispq)
  32. puts("priority queue");
  33. else
  34. puts("impossible");
  35. }
  36. }

Kattis -I Can Guess the Data Structure!的更多相关文章

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  6. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

随机推荐

  1. 自定义View实现圆角化

    目的: 1.实现自定义ReleativeLayout圆角化 实现: 1.在res目录中新建attrs.xml文件,自定义属性如下. <?xml version="1.0" e ...

  2. RemoveAll测试

    foreach (var item in procode) { var reslit = LoadData((string)item.ProductCode.Trim(), item.product_ ...

  3. When you hit a wall, just kick it in.

    Teach Yourself Programming in Ten Years. ----- Peter Norvig Teach Yourself Programming in Ten Years  ...

  4. Linux 内核链表 list.h 的使用

    Linux 内核链表 list.h 的使用 C 语言本身并不自带集合(Collection)工具,当我们需要把结构体(struct)实例串联起来时,就需要在结构体内声明指向下一实例的指针,构成所谓的& ...

  5. P3378 【模板】堆

    题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...

  6. [tyvj-1391]走廊泼水节 最小生成树

    做克鲁斯卡尔的时候维护一个并查集即可. #include <iostream> #include <cstdio> #include <cstring> #incl ...

  7. FFMpeg 常用命令格式转换,视频合成

    FFmpeg都是命令行的,用起来肯定不方便.但是,这对技术宅应该不成问题.下面,我就罗列一些比较实用的使用方法吧. FFmpeg的下载与安装 FFmpeg是开源的.但我们不必去下载它的源代码.下载已经 ...

  8. http协议的状态码(200,404,503)

    http协议的状态码 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101( ...

  9. strtotime的一个使用问题

    我在开发过程中遇到这么这个问题,因为赶进度,没有记下来处理方案,在鸟哥的博客看到原理分析,很到位!平时开发中总是急着处理问题,没有深入分析和记录问题. 1.问题: 今天是2018-07-31 执行代码 ...

  10. B - Networking

    B - Networking 思路:并查集板子. #include<cstdio> #include<cstring> #include<iostream> #in ...