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

Description

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

 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 ;
}
上一篇:linux每日命令(27):chmod命令


下一篇:VC++深入详解读书笔记-第六章菜单