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. 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 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 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 x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF).
******************************************************************************************************************
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
6
1 1 1 2 1 3 2 1 2 2 2 3
6
1 1 1 2 1 3 2 3 2 2 2 1
2
1 1 2 2
4
1 2 1 1 2 1 2 2
7
1 2 1 5 1 1 1 3 2 5 1 4 2 4
******************************************************************************************************************
Sample Output
queue
not sure
impossible
stack
priority queue
******************************************************************************************************************
题意:根据操作,判断属于那种数据结构。
思路:使用STL中对应函数,进行模拟,若矛盾则不符合。
#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int n;
while (~scanf("%d", &n))
{
stack<int>st;
queue<int>qu;
priority_queue<int>pr;
int a = 1, b = 1, c = 1;
for (int i = 0; i < n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
if (x == 1)//向其中加入数据
{
st.push(y);
qu.push(y);
pr.push(y);
}
else
{
if (st.empty())
{
a = b = c = 0;
continue;
}
if (a)
a = (st.top() == y);//判断顶部的数是否与y相等
if (b)
b = (qu.front() == y);
if (c)
c = (pr.top() == y);
st.pop();
qu.pop();
pr.pop();
}
}
if (a + b + c > 1)
printf("not sure\n");
else if (a)
printf("stack\n");
else if (b)
printf("queue\n");
else if (c)
printf("priority queue\n");
else
printf("impossible\n");
} return 0;
}
UVA - 11995 - I Can Guess the Data Structure! STL 模拟的更多相关文章
- [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 ...
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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!(ADT)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...
- 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!
做道水题凑凑题量,=_=||. 直接用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 ...
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5929 Basic Data Structure(模拟 + 乱搞)题解
题意:给定一种二进制操作nand,为 0 nand 0 = 10 nand 1 = 1 1 nand 0 = 1 1 nand 1 = 0 现在要你模拟一个队列,实现PUSH x 往队头塞入x,POP ...
随机推荐
- nodejs使用记录
安装 下载64or32的安装程序,狂点下一步,无脑安装.然后检查一下: npm 使用npm -v命令检查npm是否可用 然后我们就可以使用npm了,npm语法如下: npm install <M ...
- Swiper点击后自动轮播停止情况
用户操作swiper之后,是否禁止autoplay.默认为true:停止. 如果设置为false,用户操作swiper之后自动切换不会停止,每次都会重新启动autoplay. 操作包括触碰,拖动,点击 ...
- EF出错:Unable to convert MySQL date/time value to System.DateTime
环境: .Net 4.5 EF6 MySQL 错误提示: MySql.Data.Types.MySqlConversionException : Unable to convert MySQL dat ...
- 浅谈欧几里得算法求最大公约数(GCD)的原理及简单应用
一.欧几里得算法及其证明 1.定义: 欧几里得算法又称辗转相除法,用于求两数的最大公约数,计算公式为GCD(a,b)=GCD(b,a%b): 2.证明: 设x为两整数a,b(a>=b)的最大公约 ...
- 使用win10 hyper-v安装linux系统
1.控制面板---程序---启动或关闭windows功能---启动hyper-v管理器---重启 2.配置网络 因为公司内网通过ip验证,而通过桥接的方式,虚拟机就相当于物理机所在的网络中的一台真实主 ...
- 20、List集合中特有的方法
List里面的特有方法简介 List中除了Collection里面的方法以外,内部还有一些方法,通过这些方法,开发者可以更方便的操作List接口的实现类. package com.monkey1024 ...
- margin-bottom无效问题以及div里内容动态居中样式!
最近调前端样式时候,遇到一个需求,在中间文字不对等的情况下想让下面的操作文字距离底部对齐,如图: , 刚开始觉得使用margin-bottom就可以,后来发现只有margin-top是管用的,查了资料 ...
- angular select ng-change实时获取value
<select ng-model="vm.selectVal" ng-options="o.id as o.title for o in vm.optionsDat ...
- Callable和futrue、ExecutorService的用法
首先说明是为了解决什么问题? 为了解决主线程无谓等待浪费服务器资源的问题.当主线程执行一个费时的操作时,比如客户端发起一个请求,该请求在服务器端处理很复杂,如需要调用其他系统的接口,总之比较耗时.这时 ...
- html 列表标签
1.有序列表 <ol> <li>你好</li> <li>你好</li> <li>你好</li> </ol> ...