ACboy needs your help again!(栈和队列)HDU - 1702
作者:互联网
题目: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();
}
}
}
}
}
}
标签:again,needs,help,ACboy,cin,FIFO,strl,FILO,OUT 来源: https://blog.csdn.net/R_711/article/details/119422883