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 ...
随机推荐
- Asp.net Core 源码-SessionExtensions
using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace SportsStore.Infrastructure { publi ...
- 【JavaScript框架封装】实现一个类似于JQuery的基础框架、事件框架、CSS框架、属性框架、内容框架、动画框架整体架构的搭建
/* * @Author: 我爱科技论坛 * @Time: 20180715 * @Desc: 实现一个类似于JQuery功能的框架 * V 1.0: 实现了基础框架.事件框架.CSS框架.属性框架. ...
- Problem 11
Problem 11 # Problem_11.py """ In the 20×20 grid below, four numbers along a diagonal ...
- Problem 2
Problem 2 # Problem_2.py """ Each new term in the Fibonacci sequence is generated by ...
- Elasticsearch 入门 - Modifying Your Data
index/update/delete 均有大概1秒的缓存时间 Indexing/Replacing Documents curl -X PUT "localhost:9200/custom ...
- 洛谷—— P1238 走迷宫
https://www.luogu.org/problem/show?pid=1238 题目描述 有一个m*n格的迷宫(表示有m行.n列),其中有可走的也有不可走的,如果用1表示可以走,0表示不可以走 ...
- 洛谷 U249 匹配
U249 匹配 题目描述 输入整数s和两个整数集合A和B,从这A和B中各取一个数,如果它们的和等于s,称为“匹配”.编程统计匹配的总次数 输入输出格式 输入格式: 第一行为三个整数s(0<s≤1 ...
- [SharePoint2010开发入门经典]四、开发者常见任务
本章概要: 1.创建不同种类的web部件,包括标准的,可视化的还有数据绑定web部件 2.理解列和内容类型,如何使用它们创建列表 3.理解如何与SPS交互,使用SPS API调用数据 4.创建编辑页面 ...
- cocos2dx3.2 android平台搭建开发环境纠错备忘录
平台:win32 + android cocos2d版本号:3.2 搭建cocos2d-x android 常见问题: 问题1: Android platform not specified, sea ...
- 第一篇、Android Supersu 权限管理定制,隐藏过滤权限,指定APP最高权限
近期有个需求,在预装ROM的时候,须要权限,可是又不同意全部的应用都有权限,仅仅同意自己的应用有最高的权限(当然没有系统签名情况下). 所以.编译了CM 提取了supersu进行了二次定制,让他进行权 ...