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
6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4
1
2 1
queue
not sure
impossible
stack
priority queue
impossible

题意

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

思路

用stl模拟

代码

#include<bits/stdc++.h>
using namespace std;
int main() {
int n, a, x;
while(cin >> n) {
stack<int> s;
queue<int> q;
priority_queue<int> pq;
bool iss = , isq = , ispq = ;
while(n--) {
cin >> a >> x;
if(a == ) {
s.push(x);
q.push(x);
pq.push(x);
} else {
if(s.empty() || s.top() != x) iss = ;
if(q.empty() || q.front() != x) isq = ;
if(pq.empty() || pq.top() != x) ispq = ;
if(!s.empty()) s.pop();
if(!q.empty()) q.pop();
if(!pq.empty()) pq.pop();
}
}
if(iss + isq + ispq > )
puts("not sure");
else if(iss)
puts("stack");
else if(isq)
puts("queue");
else if(ispq)
puts("priority queue");
else
puts("impossible");
}
}

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. Project Euler 14 Longest Collatz sequence

    题意:对于任意一个数 N ,寻找在 100,0000 之内按照规则( N 为奇数 N = N * 3 + 1 ,N 为偶数 N = N / 2 ,直到 N = 1 时的步数 )步数的最大值 思路:记忆 ...

  2. 【Codeforces Round #507 (Div. 2, based on Olympiad of Metropolises) A】Palindrome Dance

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] i从1..n/2循环一波. 保证a[i]和a[n-i+1]就好. 如果都是2的话填上min(a,b)*2就好 其他情况跟随非2的. ...

  3. ARP(地址解析协议)

    目录 1. ARP 概述 2. ARP 协议工作原理 3. ARP 缓存 4. ARP 报文格式 5. 抓包分析 5.1. ARP 请求报文 5.2. ARP 应答报文 6. 免费 ARP 7. AR ...

  4. redis 零散知识

    1.单线程 2.默认 16 个库.0~15 3.select :切换数据库 4.DBsize :查看当前数据库的数量 5.keys * :查看当前库的所有 key 6.keys k? :问号是占位符 ...

  5. springMVC入门截图

    mvc在bs系统下的应用 ---------------------------------------------------- 在web.xml中配置前端控制器(系统提供的一个servlet类   ...

  6. ST Nucleo mbed套件开发 一 MBED环境使用 以Nucleo-F401为例 (二)

    MBED环境.使用起来总是那么的别扭可能很多人不习惯用在线编程器,大多数做ST32开发的都比較喜欢KEIL或者IAR,有没有什么好的方法呢.我们能够本地编译MBEDproject, 答案是肯定了.下来 ...

  7. HDU 4354

    思路是在看电视时突然想到的.枚举区间,然后按树形DP来选择最大值看是否满足条件.但枚举区间时的方法低效,看了题解,说枚举区间可以设两个指针逐步移动, 开始 l = r = 1, 记录已经出现的国家. ...

  8. hdu 1754 I Hate It 线段树 点改动

    // hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...

  9. Android recovery UI实现分析

    Android recovery模式为何物? 关于这个问题, baidu上已经有无数的答案.不理解的朋友先补习一下. 从纯技术角度来讲, recovery和android本质上是两个独立的rootfs ...

  10. nginx报 File not found 错误

    原因可能非常多,但对于刚開始学习的人.大部分应该是/etc/nginx/conf.d/default.conf里面的php解析部分配置不正确. 解决的话就是把root定义.在server下加上,这样r ...