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 ...
随机推荐
- Python基础:dict & set
一 :dict 1:Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. eg: dict查找 ...
- 【转】Oracle基础结构认知—初识oracle 礼记八目 2017-12-12 21:19:30
Oracle服务器(oracle server)由实例和数据库组成.其中,实例就是所谓的关系型数据库管理系统(Relational Database Management System,RDBMS), ...
- python简单的输入与输出
1 首先利用python完成简单的输出,运行如下: python和c语言类似,但又有所不同,python开发快,语言简洁,我是这样对比学的 输出:print+空格+'要输出的内容',一定要是英文状态下 ...
- JS 封装一个求数组最大值的函数
var aa = [1,2,3,4,9,2,5]; z(aa); function z(attr){ var b = 0 for(var i =1;i<aa.length;i++){ if(aa ...
- 这份接口管理平台 eoLinker 开源版的部署指南教程你一定不想错过
本文主要内容是讲解如何在本地部署eoLinker开源版. 环境要求 1.PHP 5.5+ / PHP7+(推荐) 2.Mysql 5.5+ / Mariadb 5.5+ 3.Nginx(推荐) / A ...
- [模板] zkw线段树
zkw线段树 code1简单版本 code2差分版本(暂无) code1:(有注释) //By Menteur_Hxy #include<cstdio> #include<iostr ...
- Emgu cv人脸检测识别
Emgu cv人脸检测识别 1.开发平台:WIN10 X64 VS2012 Emgucv版本:3.1 2.先给大家分享一个官网给的示例源代码: https://ncu.dl.sourcef ...
- 专访Bruce Douglass,谈嵌入式经验
Bruce:表面上看,编程就是想要实现什么就写什么代码:但事实是,敲代码只是软件开发过程中很小的一部分,程序员的工作还包括安全分析.责任分析.产品验证.产品分析等. =========== ...
- 如何计算合适的InnoDB log file size
原文链接:http://www.mysqlperformanceblog.com/2008/11/21/how-to-calculate-a-good-innodb-log-file-size/ Pe ...
- poj 2914&&hdu 3002 全局最小割Stoer-Wagner算法模板
#include<stdio.h> #include<string.h> #include<iostream> #define inf 0x3fffffff #de ...