Kattis -I Can Guess the Data Structure!
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 |
---|---|
|
|
题意
模拟栈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!的更多相关文章
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- 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 ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ 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 - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- 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 ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
随机推荐
- 自定义View实现圆角化
目的: 1.实现自定义ReleativeLayout圆角化 实现: 1.在res目录中新建attrs.xml文件,自定义属性如下. <?xml version="1.0" e ...
- RemoveAll测试
foreach (var item in procode) { var reslit = LoadData((string)item.ProductCode.Trim(), item.product_ ...
- When you hit a wall, just kick it in.
Teach Yourself Programming in Ten Years. ----- Peter Norvig Teach Yourself Programming in Ten Years ...
- Linux 内核链表 list.h 的使用
Linux 内核链表 list.h 的使用 C 语言本身并不自带集合(Collection)工具,当我们需要把结构体(struct)实例串联起来时,就需要在结构体内声明指向下一实例的指针,构成所谓的& ...
- P3378 【模板】堆
题目描述 如题,初始小根堆为空,我们需要支持以下3种操作: 操作1: 1 x 表示将x插入到堆中 操作2: 2 输出该小根堆内的最小数 操作3: 3 删除该小根堆内的最小数 输入输出格式 输入格式: ...
- [tyvj-1391]走廊泼水节 最小生成树
做克鲁斯卡尔的时候维护一个并查集即可. #include <iostream> #include <cstdio> #include <cstring> #incl ...
- FFMpeg 常用命令格式转换,视频合成
FFmpeg都是命令行的,用起来肯定不方便.但是,这对技术宅应该不成问题.下面,我就罗列一些比较实用的使用方法吧. FFmpeg的下载与安装 FFmpeg是开源的.但我们不必去下载它的源代码.下载已经 ...
- http协议的状态码(200,404,503)
http协议的状态码 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 100(继续) 请求者应当继续提出请求.服务器返回此代码表示已收到请求的第一部分,正在等待其余部分. 101( ...
- strtotime的一个使用问题
我在开发过程中遇到这么这个问题,因为赶进度,没有记下来处理方案,在鸟哥的博客看到原理分析,很到位!平时开发中总是急着处理问题,没有深入分析和记录问题. 1.问题: 今天是2018-07-31 执行代码 ...
- B - Networking
B - Networking 思路:并查集板子. #include<cstdio> #include<cstring> #include<iostream> #in ...