HDU - 1702 ACboy needs your help again!(栈和队列)
Description
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!
Input
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
Output
Sample Input FIFO
IN
IN
OUT
OUT
FILO
IN
IN
OUT
OUT
FIFO
IN
IN
OUT
OUT
OUT
FILO
IN
IN
OUT
IN
OUT
Sample Output None
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1702
*******************************************
题意:题意比较清晰,FIFO则先进先出,FILO则先进后出
分析:简单的队列和栈的应用就好
AC代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#include<time.h>
#include<stack>
using namespace std;
#define N 1000
#define INF 0x3f3f3f3f int main()
{
int T,i,a,n;
char s[N],str[N]; scanf("%d", &T); while(T--)
{
scanf("%d %s", &n , s); if(s[]=='F')///存入队列
{
queue<int >Q;
for(i=;i<n;i++)
{
scanf("%s", str);
if(str[]=='I')
{
scanf("%d", &a);
Q.push(a);
}
else
{
if(Q.empty())
printf("None\n");
else
{
int x=Q.front();
Q.pop();
printf("%d\n", x);
}
}
}
}
else
{
stack<int >P;
for(i=;i<n;i++)
{
scanf("%s", str); if(str[]=='I')
{
scanf("%d",&a);
P.push(a);
}
else
{
if(P.empty())
printf("None\n");
else
{
int x=P.top();
P.pop();
printf("%d\n", x);
}
}
}
}
} return ;
}
HDU - 1702 ACboy needs your help again!(栈和队列)的更多相关文章
- hdu 1702 ACboy needs your help again!
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1702 ACboy needs your help again! Description ACboy w ...
- (hdu step 8.1.1)ACboy needs your help again!(STL中栈和队列的基本使用)
题目: ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1712 ACboy needs your help(包背包)
HDU 1712 ACboy needs your help(包背包) pid=1712">http://acm.hdu.edu.cn/showproblem.php? pid=171 ...
- queue的使用-Hdu 1702
ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...
- 剑指Offer面试题:6.用两个栈实现队列
一.题目:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 原文是使用 ...
- C实现栈和队列
这两天再学习了数据结构的栈和队列,思想很简单,可能是学习PHP那会没有直接使用栈和队列,写的太少,所以用具体代码实现的时候出现了各种错误,感觉还是C语言功底不行.栈和队列不论在面试中还是笔试中都很重要 ...
- JavaScript数组模拟栈和队列
*栈和队列:js中没有真正的栈和队列的类型 一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭 FILO 何时使用:今后只要仅希望数组只能从一端进 ...
随机推荐
- Openjudge-计算概论(A)-完美立方
描述: a的立方 = b的立方 + c的立方 + d的立方为完美立方等式.例如12的立方 = 6的立方 + 8的立方 + 10的立方 .编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组 ...
- windows服务-log4net的使用
本文转自http://www.cnblogs.com/puzi0315/archive/2012/08/08/2628966.html Log4net监控服务状态 对于比较复杂的逻辑,可以使用log4 ...
- Html的Table与Echart的饼图实现联动效果
功能描述: 单击Table中的某个单元格,Echart的饼图加载相关的数据,鼠标悬停在Echarts饼图中的某一块中,Table显示与Echarts饼图相关的数据. 例:楼宇经济概要显示每一个季度所有 ...
- Raft协议详解-leader发送心跳代码go
如果已经把最新的log更新了,那就多等一会,反之,很快就广播AppendEntries(也就是心跳消息) func (rf *Raft) LeaderState() { time.Sleep(10 * ...
- C++侵入式链表
C++标准模板库中的list是非侵入式的链表,当我们通过对象来删除容器中的对象时,需要从头到尾查找一次得到iterator,最后通过iterator来删除对象.这样删除容器中的对象时比较缓慢,所以就实 ...
- svn hooks的使用demo
我是理论家: svn server端提供了Hooks Script.所谓钩子实际上是一种时间触发机制,是指当系统执行到某个特殊的事件时,触发我们预定义的动作,可以让我们在某些特定状态发生的时候做我们想 ...
- HttpClient的get和post方式提交数据的使用
/** * Http工具类 */ public class HttpUtil { // 创建HttpClient对象 public static HttpClient httpClient = new ...
- 如何让linux时间与internet时间同步(centos)
笔者在使用linux时(虚拟机),经常会发现使用一段时间后,linux时间和我的宿主机(真实机)的时间不一致,而宿主机的时间确实是internet时间,安装linux时选择的时区也是Asia/Shan ...
- Chapter 1 First Sight——28
"Which one is the boy with the reddish brown hair?" 那个红褐色头发的男孩是谁? I asked. I peeked at him ...
- 全选js实现
前端全选框 <input type='checkbox' name='allSelect' onclick='sel(this)'></input> //有i个选择框 < ...