UVA-11995
There is a bag-like data structure, supporting two operations:1 x Throw an element x into the bag.2 Take out an element from the bag.Given a sequence of operations with return values, you’re going to guess the data structure. It isa stack (Last-In, First-Out),
a queue (First-In, First-Out), a priority-queue (Always take out largerelements first) or something else that you can hardly imagine!InputThere are several test cases. Each test case begins with a line containing a single integer n (1 ≤ n ≤1000). Each of the
next n lines is either a type-1 command, or an integer 2 followed by an integer x.That means after executing a type-2 command, we get an element x without error. The value of xis always a positive integer not larger than 100. The input is terminated by end-of-file
(EOF).OutputFor 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 mentionedabove.Sample Input61 11 21 32 12 22 361 11 21 32 32 22 121 12 241 21 12 12 271 21 51 11 32 51 42 4Sample Outputqueuenot sureimpossiblestackpriority queue
题解:分别定义 stack、queue、priority_queue 判断这个操作序列是不是符合上述结构。
AC代码为:
#include<stdio.h>
#include<stack>
#include<queue>
using namespace std;
int main()
{
int n, i, x, y, f[4];
while(~scanf("%d",&n))
{
stack<int> s;
queue<int> q;
priority_queue<int, vector<int>, less<int> > pq;
for(i=0;i<3;i++)
f[i]=1;
for(i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
if(x == 1)
{
s.push(y);
q.push(y);
pq.push(y);
}
else
{
if(!s.empty())
{
if(s.top() != y)
f[0] = 0;
s.pop();
}
else
f[0] = 0;
if(!q.empty())
{
if(q.front() != y)
f[1] = 0;
q.pop();
}
else
f[1] = 0;
if(!pq.empty())
{
if(pq.top() != y)
f[2] = 0;
pq.pop();
}
else
f[2] = 0;
}
}
int num = 0;
for(i = 0; i < 3; i++)
if(f[i] == 1)
num++;
if(num == 0)
printf("impossible\n");
else if(num > 1)
printf("not sure\n");
else
{
if(f[0] == 1)
printf("stack\n");
else if(f[1] == 1)
printf("queue\n");
else
printf("priority queue\n");
}
}
return 0;
}
UVA-11995的更多相关文章
- [UVA] 11995 - I Can Guess the Data Structure! [STL应用]
11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...
- STL UVA 11995 I Can Guess the Data Structure!
题目传送门 题意:训练指南P186 分析:主要为了熟悉STL中的stack,queue,priority_queue,尤其是优先队列从小到大的写法 #include <bits/stdc++.h ...
- UVa 11995:I Can Guess the Data Structure!(数据结构练习)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...
- UVa 11995 I Can Guess the Data Structure!
做道水题凑凑题量,=_=||. 直接用STL里的queue.stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空. #include ...
- uva 11995 I Can Guess the Data Structure stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- UVA 11995 I Can Guess the Data Structure!(ADT)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- UVA - 11995 - I Can Guess the Data Structure! STL 模拟
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
- UVA - 11995 模拟
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...
- UVA 11995 STL 使用
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
随机推荐
- java VS c#,异同点
因工作安排,后期需要维护一个java项目.所以稍微熟悉下java,开此篇记录下java与c#的区别点,方便增强自己学习效果.肯定是不全的,可能是有错的,欢迎批评指正. 一.关键字 描述 C# Java ...
- 更改微信小程序的组件默认样式
checkbox /*checkbox 整体大小 */ .checkbox { width: 12%; /* height: 240rpx; */ } /*checkbox 选项 ...
- poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35999 Accepted: 13145 ...
- 基于.NetStandard的简易EventBus实现-基础实现
一.问题背景 最近离职来到了一家新的公司,原先是在乙方工作,这回到了甲方,在这一个月中,发现目前的业务很大一部分是靠轮询实现的,例如:通过轮询判断数据处于B状态了,则轮询到数据后执行某种动作,这个其实 ...
- 记录工作遇到的死锁问题(Lock wait timeout exceeded; try restarting transaction)
1.问题背景 刚来新公司不久,对业务还不太熟悉,所以领导先安排我维护原有系统.大概介绍下项目背景,项目分为核心业务部分在项目A中,与第三方交互的业务在项目B中,前端发起请求调用A项目接口,并在A项目中 ...
- 构思一个在windows下仿objc基于动画层ui编程的ui引擎
用c/c++编程有些年了,十个指头可以数齐,在涉入iOS objc开发后,有种无比舒服的感觉,尤其在UI开发上. 在QuartzCore.framework下动画和透明窗口等许多效果的事都变得那么方便 ...
- ACE框架 基于共享内存的分配器 (算法设计)
继承上一篇<ACE框架 基于共享内存的分配器设计>,本篇分析算法部分的设计. ACE_Malloc_T模板定义了这样一个分配器组件 分配器组件聚合了三个功能组件:同步组件ACE_LOCK, ...
- 利用Python学习线性代数 -- 1.1 线性方程组
利用Python学习线性代数 -- 1.1 线性方程组 本节实现的主要功能函数,在源码文件linear_system中,后续章节将作为基本功能调用. 线性方程 线性方程组由一个或多个线性方程组成,如 ...
- 部署helm服务
helm在ocp中相当于catalog中的template k8s中使用helm之前遇到的问题 .很难管理.编辑和维护如此多的服务.每个服务都有若干配置,缺乏一个更高层次的工具将这些配置组织起来. . ...
- [Odoo12基础教程]之win10中odoo12环境搭建
所需材料 1.python3.7 2.pycharm社区版及以上 3.postgresSQL10 下载链接:https://www.enterprisedb.com/thank-you-downloa ...