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 |
|---|---|
6 |
queue |
题意
模拟栈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 ...
随机推荐
- MongoDB经典入门(2)--shell命令
MongoDB是MongoDB自带的交互式Javascript shell,用来对MongoDB进行操作和管理的交互式环境. 使用 "./mongo --help" 可查看相关连接 ...
- websocket 进阶!netty框架实现websocket达到高并发
引言: 在前面两篇文章中,我们对原生websocket进行了了解,且用demo来简单的讲解了其用法.但是在实际项目中,那样的用法是不可取的,理由是tomcat对高并发的支持不怎么好,特别是tomcat ...
- js replace 全部替换
js 将字符串中指定字符全局替换 语法 stringObject.replace(regexp/substr, replacement) 它将在 stringObject 中查找与 regexp 相匹 ...
- 2、Ansible配置文件详解
0.配置文件 两个核心文件:ansible.cfg和hosts文件,默认都存放在/etc/ansible目录下. ansible.cfg:主要设置一些ansible初始化的信息,比如日志存放路径.模块 ...
- 常见的dos命令(及抒写方式)
1.dir:列出当前目录下的主体及文件夹. 2.md:创建目录. 3.rd:删除目录.{注意:rd不能删除非空的文件夹,并且只能用于文件夹的删除} 3.cd :进入指定目录. 4.cd . . :退出 ...
- 1.2 为Eclipse绑定Tomcat
1.window→preferences打开属性窗口 2.点击add 3.点击Tomcat6.0 点击next 4.选择tomcat的解压目录和jdk,并点击finish 5.点击ok 6.打开ser ...
- jsp三层架构
学了.net了,它的三层架构很好用.现在学jsp,我们一样可以用三层架构来开发.下面详细介绍 1.创建数据库 drop table MyUser create table MyUser ( id ,1 ...
- cliendataset中自增长字段的处理
cliendataset中自增长字段的处理: id:自增长字段. 在client中的处理方法:clientdataset.Fields.FieldByName('id').ReadOnly:=Fals ...
- 洛谷 U5737 纸条
U5737 纸条 题目背景 明明和牛牛是一对要好的朋友,他们经常上课也想讲话,但是他们的班级是全校纪律最好的班级,所以他们只能通过传纸条的方法来沟通.但是他们并不能保证每次传纸条老师都无法看见,所以他 ...
- Libevent学习笔记
学习: /Users/baidu/Documents/Data/Interview/服务器-检索端/libevent参考手册(中文版).pdf 讲的不好.翻译的..