ACboy needs your help again!
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5685 Accepted Submission(s): 2973
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!
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
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<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack> //栈
#include<queue> //队列
using namespace std;
queue <int> q; //定义队列q
stack <int> s; //定义栈s
int i,n,m,j,k,l;
char c[100],ch[100];
int main()
{
scanf("%d",&n);
while(n--)
{
while(!s.empty()) //清空栈s
s.pop();
while(!q.empty()) //清空队列q
q.pop();
scanf("%d %s",&m,c);
if(strcmp(c,"FIFO")==0){
for(i=0;i<m;i++)
{
scanf("%s",ch);
if(strcmp(ch,"IN")==0)
{
scanf("%d",&k);
q.push(k); //将k读入队列q
}
else
{
if(!q.empty()) //判断队列q是否为空
{
printf("%d\n",q.front()); //将队列q按先进先出的顺序输出 front指队列最前边(最先读入的数字)的数
q.pop(); // 将队列最前边的数字输出后,那个数字仍然在队列里边,,,用q.pop();将输出的数抛出队列
}
else
printf("None\n");
}
}
}
else
{
for(i=0;i<m;i++)
{
scanf("%s",ch);
if(strcmp(ch,"IN")==0)
{
scanf("%d",&k);
s.push(k); //将k读入栈s
}
else
{
if(!s.empty())
{
printf("%d\n",s.top()); //将栈按先进后出的顺序输出 top指栈的最上边的数(最后读入的数)
s.pop(); //将输出的数抛出栈
}
else
printf("None\n");
}
}
}
}
return 0;
}// 杭电1702ACboy needs your help again! 栈和队列简单应用的模板
896


&spm=1001.2101.3001.11974&articleId=52059101&d=1&t=3&u=16a06e15a7a44d9a8a238898545c49c3)

被折叠的 条评论
为什么被折叠?



