UVa 11995 I Can Guess the Data Structure!
做道水题凑凑题量,=_=||。
直接用STL里的queue、stack 和 priority_queue模拟就好了,看看取出的元素是否和输入中的相等,注意在此之前要判断一下是否非空。
- #include <bits/stdc++.h>
- using namespace std;
- void scan( int &x )
- {
- char c;
- while( c = getchar(), c < '' || c > '' );
- x = c - '';
- while( c = getchar(), c >= '' && c <= '' ) x = x* + c - '';
- }
- const int maxn = + ;
- int t[maxn], d[maxn];
- bool is_queue(int n)
- {
- queue<int> Q;
- for(int i = ; i < n; i++)
- {
- if(t[i] == ) Q.push(d[i]);
- else
- {
- if(Q.empty()) return false;
- int x = Q.front(); Q.pop();
- if(x != d[i]) return false;
- }
- }
- return true;
- }
- bool is_stack(int n)
- {
- stack<int> S;
- for(int i = ; i < n; i++)
- {
- if(t[i] == ) S.push(d[i]);
- else
- {
- if(S.empty()) return false;
- int x = S.top(); S.pop();
- if(x != d[i]) return false;
- }
- }
- return true;
- }
- bool is_p_queue(int n)
- {
- priority_queue<int> Q;
- for(int i = ; i < n; i++)
- {
- if(t[i] == ) Q.push(d[i]);
- else
- {
- if(Q.empty()) return false;
- int x = Q.top(); Q.pop();
- if(x != d[i]) return false;
- }
- }
- return true;
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- int n;
- while(scanf("%d", &n) == )
- {
- for(int i = ; i < n; i++) { scan(t[i]); scan(d[i]); }
- bool f1 = is_queue(n);
- bool f2 = is_stack(n);
- bool f3 = is_p_queue(n);
- int cnt = (int)f1 + (int)f2 + (int)f3;
- if(cnt > ) puts("not sure");
- else if(cnt == ) puts("impossible");
- else
- {
- if(f1) puts("queue");
- if(f2) puts("stack");
- if(f3) puts("priority queue");
- }
- }
- return ;
- }
代码君
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 stack,queue,priority_queue
题意:给你n个操做,判断是那种数据结构. #include<iostream> #include<cstdio> #include<cstdlib> #includ ...
- UVA - 11995 I Can Guess the Data Structure!(模拟)
思路:分别定义栈,队列,优先队列(数值大的优先级越高).每次放入的时候,就往分别向三个数据结构中加入这个数:每次取出的时候就检查这个数是否与三个数据结构的第一个数(栈顶,队首),不相等就排除这个数据结 ...
- 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 ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
随机推荐
- ssh连接慢
suse刚装完,开始用ssh的时候,总会遇到这样的问题:输入了用户名以后,等半天才出输入密码的框,很是急人.这是dns反查造成的.解决方法:编辑 /etc/ssh/sshd_conf , 将 #Use ...
- 原码 & 反码 & 补码 & 详解
本篇文章讲解了计算机的原码, 反码和补码. 并且进行了深入探求了为何要使用反码和补码, 以及更进一步的论证了为何可以用反码, 补码的加法计算原码的减法. 论证部分如有不对的地方请各位牛人帮忙指正! 希 ...
- win8 ubuntu
点进去看到几点注意: 1. 如果Windows是UEFI方式安装的,那Ubuntu必须也用UEFI方式安装 2. 必须用64位的Ubuntu安装文件,32位的不能探测EFI 3. 必须用UEFI的方式 ...
- 如何监控业务的响应速度?Cloud Insight SDK 实践分享
一直在说 Cloud Insight 是数据聚合平台,可以用 SDK 和 API 实现业务监控,如今不拿出点实践人们恐怕是不能信服.那今天本文就先简单介绍一下 SDK 可以应用在哪些方面,再举个真实用 ...
- js正则函数match、exec、test、search、replace、split使用介绍
match() 使用正则表达式模式对字符串执行查找,并将包含查找的结果作为数组返回. stringObj.match(rgExp) stringObj 必选项.对其进行查找的 String 对象或字符 ...
- 李洪强iOS开发之【零基础学习iOS开发】【01-前言】01-开篇
从今天开始,我就开始更新[零基础学习iOS开发]这个专题.不管你是否涉足过IT领域,也不管你是理科生还是文科生,只要你对iOS开发感兴趣,都可以来阅读此专题.我尽量以通俗易懂的语言,让每个人都能够看懂 ...
- python 解析 xml
<taskList nextId="62292"> <task module="reliability" owner="vprovo ...
- javascript基础之数组对象
一.定义数组的方法: 定义了一个空数组: var myArray =new Array(); 指定有n个空元素的数组: var myArray=new Array(n); 定义数组并赋值: var m ...
- 多线程进行n皇后计算
在浏览zhihu的时候, 看到了这个问题:Linux c++服务器端这条线怎么走? http://www.zhihu.com/question/22608820 , 其中排第一的答案说的很不错.针对他 ...
- 243. Shortest Word Distance
题目: Given a list of words and two words word1 and word2, return the shortest distance between these ...