ACboy needs your help again!(栈和队列)HDU - 1702

该博客主要介绍了ACboy被怪物困于迷宫的故事,以此为背景提出了一个关于FIFO(先进先出)和FILO(先进后出)概念的问题。ACMer需要通过解决一系列基于这两个概念的问题来救出ACboy。输入包含多个测试用例,每个用例描述了命令序列,要求输出根据FIFO或FILO规则处理后的结果。博主通过代码实现了这个问题的解决方案,演示了如何处理输入并给出正确答案。

题目:ACboy needs your help again!

ACboy was kidnapped!!
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 input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
Output
For each command “OUT”, you should output a integer depend on the word is “FIFO” or “FILO”, or a word “None” if you don’t have any integer.
Sample Input
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT
Sample Output
1
2
2
1
1
2
None
2
3

中文大意

阿奇博被绑架了!!
他非常想念他的母亲,现在非常害怕。你无法想象他被放进的房间有多黑, :(太差了。
作为一个聪明的ACMer,你想让ACboy走出怪物的迷宫。但是当你到达迷宫的门口时,蒙斯特说:“我听说你很聪明,但如果不能解决我的问题,你会和ACboy一起死去。
怪物的问题显示在墙上:
每个问题的第一行是整数N(命令的数量),以及一个单词"FIFO"或"FILO”。(你很高兴,因为你知道"FIFO"代表"先出",而"FILO"的意思是"先出最后")。
和以下 N 行,每行是"IN M"或"OUT",(M 表示整数)。
问题的答案是一扇门的通行证, 所以如果你想拯救 Acboy, 仔细回答问题!
输入
输入包含多个测试案例。
第一行有一个整数,表示测试案例的数量。
每个子问题的输入都在上面描述。
输出
对于每个命令"OUT",您应该输出一个整数,取决于单词是"FIFO"或"FILO",或者一个单词"无",如果你没有任何整数。
示例输入
4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT
样本输出
1
2
2
1
1
2
None
2
3

实现代码:

//标准模板题
#include<bits/stdc++.h>
using namespace std;
int n,t;
string str;
string strl;

int main()
{
	cin>>n;
	while(n--)
	{
		stack <int> b;
		queue <int> a;
		int tp;
		cin>>t>>str;
		if(str=="FIFO")
		{
			for(int i=0; i<t; i++)
			{
				cin>>strl;
				if(strl=="IN")
				{
					cin>>tp;
					a.push(tp);
				}
				if(strl=="OUT")
				{
					if(a.empty())
						cout<<"None"<<endl;
					else
					{
						cout<<a.front()<<endl;
						a.pop();
					}
				}
			}
		}
		if(str=="FILO")
		{
			for(int i=0; i<t; i++)
			{
				cin>>strl;
				if(strl=="IN")
				{
					cin>>tp;
					b.push(tp);
				}
				if(strl=="OUT")
				{
					if(b.empty())
						cout<<"None"<<endl;
					else
					{
						cout<<b.top()<<endl;
						b.pop();
					}
				}
			}
		}
	}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值