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
题意:FIFO是先进先出,FILO是先进后出。给一个n和一个命令(FIFO和FILO),下面n条语句,IN a代表a进去了,当输入OUT是你说出出来的是哪一个,如果此时没有元素的话输出None。
用队列和栈写比较简单写。
 1 #include<iostream>
 2 #include<stack>
 3 #include<stdio.h>
 4 #include<algorithm>
 5 #include<string.h>
 6 #include<queue>
 7 #define N 100010
 8 using namespace std;
 9 #define oo 0x3f3f3f
10 int dp[N];
11 int a;
12 char str[N];
13 char s[N];
14 int main()
15 {
16     int t,n;
17     scanf("%d",&t);
18     while(t--)
19     {
20         scanf("%d%s",&n,str);
21         if(strcmp(str,"FIFO")==0)
22         {
23             queue<int >Q;
24             while(n--)
25             {
26                 scanf("%s",s);
27                 if(s[0]=='I')
28                 {
29                     scanf("%d",&a);
30                     Q.push(a);
31                 }
32                 else
33                 {
34                     if(!Q.size()) printf("None\n");
35                     else
36                     {
37                         int k = Q.front();
38                         Q.pop();
39 
40                         printf("%d\n",k);
41                     }
42 
43                 }
44             }
45         }
46         else
47         {
48             stack<int >p;
49             while(n--)
50             {
51                 scanf("%s",s);
52                 if(s[0]=='I')
53                 {
54                     scanf("%d",&a);
55                     p.push(a);
56                 }
57                 else
58                 {
59                     if(!p.size()) printf("None\n");
60                     else
61                     {
62                         int k = p.top();
63                         p.pop();
64                         printf("%d\n",k);
65                     }
66 
67                 }
68             }
69         }
70 
71 
72     }
73     return 0;
74 }
View Code

 

posted @ 2016-08-19 10:44  biu~biu~biu~  阅读(172)  评论(0编辑  收藏  举报