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 ...
随机推荐
- python_如何在一个for循环中迭代多个可迭代对象?
案例: 某班学生期末考试成绩,语文.数学.英语分别存储在3个列表中,同时迭代三个列表.,计算每个学生的总分(并行) 某年级有4个班,某次英语成绩分别记录在4个列表中,依次迭代每个列表,统计全年级高于9 ...
- tp5中设置指定的log日志,可单独建立文件夹和文件名
1:在D:\www\tp5\thinkphp\library\think\Log.php中添加下列代码.可在runtime文件夹下建立tlogs文件夹(可自定义). /** * [payLog 支付日 ...
- maven(项目管理工具系列 maven 总结二)
♣maven是什么? ♣maven下载.安装 ♣了解maven仓库 ♣eclipse配置maven ♣创建maven项目 ♣把maven项目转化为web项目 1.maven是什么? Maven是一个项 ...
- Mybatis (一)
1 DAO层框架 框架:是一种整体的解决方案. 1.1 JDBC的步骤 1.2 Hibernate执行的步骤 1.3 MyBaits 2 Mybatis简介 Mybatis是支持定制化SQL.存储过程 ...
- 新awk整理
总感觉上一篇awk的总结几乎是照着man翻译过来的,惨不忍睹 无意间在互联网上有找到了宝贵的资料 感觉整理的很好,想着照着这个来重新写下,对照新的man更新下吧,只是总是在改变的 一.awk简介二.a ...
- XML系列之--Linq操作带属性的XML(四)
关于XML,之前解析过电文收发方面的,就是所谓的带表头.前缀(命名空间)SOAP格式.这次需求是解析一个xml的模板(xls内容),然后填充数据,最后保存.需要时可转换xls.pdf等文件.关于这种带 ...
- js 判断一个文本框是否获得焦点
1.js 判断一个文本框是否获得焦点 // 可以用document.activeElement判断 // document.activeElement表示当前活动的元素 // 查找你要判断的文本框 ...
- appium问题整理
在刚进入appium的世界时,遇到无数的坑,趟过无数的浑水,现在整理一些常用的报错讯息,供大家参考 1.org.openqa.selenium.remote.UnreachableBrowserExc ...
- 系统uid在1-499的原因
1.因为是保留给系统使用的UID,为了与用户设置的账户区分,防止冲突. 2.并没有其他特别的意义, 3.也叫作虚拟用户,除了0之外,所有的UID在使用上并没有任何区别. 4.linux中文件和程序都要 ...
- yum安装centos系统依赖库
安装centos系统依赖库,安装软件过程中,经常需要的一些库,可以在编译安装软件前执行如下命令: 首先更新系统(这步可以不执行) yum -y update 这种更新是全部更新,但是有时一些软件不想更 ...