UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高)。每次放入的时候,就往分别向三个数据结构中加入这个数;每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结构,可以用三个标志变量来标志可能的数据结构。
注意:可能会出现数据结构为空的情况,要先判断非空才能取元素。
AC代码
#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;
//分别定义栈,队列,优先队列,然后模拟
stack<int> sta;
queue<int> que;
priority_queue<int> p_que;
void init() {
while(!sta.empty()) sta.pop();
while(!que.empty()) que.pop();
while(!p_que.empty()) p_que.pop();
}
int main() {
int n;
while(scanf("%d", &n) == 1) {
init();
int tag1 = 1, tag2 = 1, tag3 = 1;
for(int i = 0; i < n; ++i) {
int tag, num;
scanf("%d %d", &tag, &num);
if (tag == 1) {
sta.push(num);
que.push(num);
p_que.push(num);
} else if(tag == 2) {
if (sta.empty() || sta.top() != num) {
tag1 = 0;
} else sta.pop();
if (que.empty() || que.front() != num) {
tag2 = 0;
} else que.pop();
if (p_que.empty() || p_que.top() != num) {
tag3 = 0;
} else p_que.pop();
}
}
int res = tag1 + tag2 + tag3;
if (res == 0) {
printf("impossible\n");
} else if(res >= 2) {
printf("not sure\n");
} else {
if (tag1) {
printf("stack\n");
} else if (tag2) {
printf("queue\n");
} else printf("priority queue\n");
}
}
return 0;
}
如有不当之处欢迎指出!
UVA - 11995 I Can Guess the Data Structure!(模拟)的更多相关文章
- [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!(数据结构练习)
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 ...
- 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. ...
- 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 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)
11995 - I Can Guess the Data Structure! There is a bag-like data structure, supporting two operation ...
随机推荐
- OpenCv函数学习(一)
Intel Image Processing Library (IPL) typedef struct _IplImage { int nSize; /* IplImage大小 */ int ID; ...
- java里程碑之泛型--深入理解泛型
所谓泛型,就是允许在定义类,接口,方法时使用类型形参,这个类型形参将在声明变量,创建对象,调用方法的时候动态的指定.JAVA5之后修改了集合中所有的接口和类,为这些接口和类都提供了泛型的支持. 关于泛 ...
- 准备:新V8即将到来,Node.js的性能正在改变
V8的Turbofan的性能特点将如何对我们优化的方式产生影响 审阅:来自V8团队的Franziska Hinkelmann和Benedikt Meurer. **更新:Node.js 8.3.0已经 ...
- 用户 'IIS APPPOOL\.NET v4.5 Classic' 登录失败。
我在win8.1系统下用vs2013+SqlServer08编写完项目后,挪到另一台win8.1系统(安装了Vs2010+SqlServer08)中,把网址挂到IIs中时,出现如下错误 : 解决方案: ...
- 【django之orm小练习】
作业1 创建单表Book表,要求字段: 1 主键 nid 2 书名 title 3 价格 price 4 出版日期 pubDate 5 出版社 publisher(普通字符串字段) class Boo ...
- iOS-Mac Charles抓包工具的使用【Mac 抓包工具Charles】
1.下载文件 Charles安装包以及破解文件下载地址:http://charles.iiilab.com 2.安装及使用 使用介绍 http://www.cocoachina.com/ios/201 ...
- vsftpd安装和使用 Linux系统和window系统
vsftpd 安装(Linux)一.安装系统环境 centos 6.9 64位二.vsftpd版本 vsftpd-2.2.2-24.el6.x86_64三.安装步骤1.安装 执行 yum -y ins ...
- RAID知识总结[转]
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://815632410.blog.51cto.com/1544685/1394306 ...
- BZOJ 2073: [POI2004]PRZ [DP 状压]
传送门 水题不解释 这道题的主要目的在于记录一个枚举子集的技巧 #include <iostream> #include <cstdio> #include <cstri ...
- BZOJ 3262: 陌上花开 [CDQ分治 三维偏序]
Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要美丽,当 ...