ACboy needs your help again!
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10013 Accepted Submission(s): 5017
Problem 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!
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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
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 3 4 5 6 7 8 9 |
1 2 2 1 1 2 None 2 3 |
简单的栈和队列的应用。然而我既不会用队列也不会用栈,只好手动实现这一些功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <istream> #define forr(a,b,c) for(int a=b;a<=c;a++) #define MAX(a,b) (a>b)?a:b; #define MIN(a,b) (a<b)?a:b; #define memsett(a) memset(a,sizeof(a),0); using namespace std; string s; int a[1002]; int main() { int t; scanf("%d", &t); getline(cin, s); forr(q,1,t){ memsett(a); getline(cin, s); int typea; int num = 0; int cachea = 0; forr(i, 0, s.length() - 1) { if ((s[i] == 'I') && (s[i + 1] == 'F')) { typea = 1; break; }//先进先出 if ((s[i] == 'I') && (s[i + 1] == 'L')) { typea = 2; break; }//先进后出 if ((s[i] >= '0') && (s[i] <= '9'))cachea = cachea * 10 + (s[i] - '0'); } if (typea == 1) { int in = 0; int out = 0; forr(w, 1, cachea) { getline(cin, s); if (s[0] == 'I') { int cacheb = 0; forr(i, 3, s.length() - 1) if ((s[i] >= '0') && (s[i] <= '9'))cacheb = cacheb * 10 + (s[i] - '0'); in++; a[in] = cacheb; } if (s[0] == 'O') { if (out + 1 > in) { printf("None\n"); continue; } out++; printf("%d\n", a[out]); } } } if (typea == 2) { int in = 0; forr(w, 1, cachea) { getline(cin, s); if (s[0] == 'I') { int cacheb = 0; forr(i, 3, s.length() - 1) if ((s[i] >= '0') && (s[i] <= '9'))cacheb = cacheb * 10 + (s[i] - '0'); in++; a[in] = cacheb; } if (s[0] == 'O') { if (in == 0) { printf("None\n"); continue; } printf("%d\n", a[in]); in--; } } } } } |