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. Asp.net Core 源码-SessionExtensions

    using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace SportsStore.Infrastructure { publi ...

  2. 【JavaScript框架封装】实现一个类似于JQuery的基础框架、事件框架、CSS框架、属性框架、内容框架、动画框架整体架构的搭建

    /* * @Author: 我爱科技论坛 * @Time: 20180715 * @Desc: 实现一个类似于JQuery功能的框架 * V 1.0: 实现了基础框架.事件框架.CSS框架.属性框架. ...

  3. Problem 11

    Problem 11 # Problem_11.py """ In the 20×20 grid below, four numbers along a diagonal ...

  4. Problem 2

    Problem 2 # Problem_2.py """ Each new term in the Fibonacci sequence is generated by ...

  5. Elasticsearch 入门 - Modifying Your Data

    index/update/delete 均有大概1秒的缓存时间 Indexing/Replacing Documents curl -X PUT "localhost:9200/custom ...

  6. 洛谷—— P1238 走迷宫

    https://www.luogu.org/problem/show?pid=1238 题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走 ...

  7. 洛谷 U249 匹配

    U249 匹配 题目描述 输入整数s和两个整数集合A和B,从这A和B中各取一个数,如果它们的和等于s,称为“匹配”.编程统计匹配的总次数 输入输出格式 输入格式: 第一行为三个整数s(0<s≤1 ...

  8. [SharePoint2010开发入门经典]四、开发者常见任务

    本章概要: 1.创建不同种类的web部件,包括标准的,可视化的还有数据绑定web部件 2.理解列和内容类型,如何使用它们创建列表 3.理解如何与SPS交互,使用SPS API调用数据 4.创建编辑页面 ...

  9. cocos2dx3.2 android平台搭建开发环境纠错备忘录

    平台:win32 + android cocos2d版本号:3.2 搭建cocos2d-x android 常见问题: 问题1: Android platform not specified, sea ...

  10. 第一篇、Android Supersu 权限管理定制,隐藏过滤权限,指定APP最高权限

    近期有个需求,在预装ROM的时候,须要权限,可是又不同意全部的应用都有权限,仅仅同意自己的应用有最高的权限(当然没有系统签名情况下). 所以.编译了CM 提取了supersu进行了二次定制,让他进行权 ...