(Hdu step 8.1.1) ACboy needs your help again! (Basic use of stacks and queues in STL), hduacboy
Question:
ACboy needs your help again! |
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission (s): 73 Accepted Submission (s): 57 |
|
Problem DescriptionACboy 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 you 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! |
InputThe 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 Input44 FIFOIN 1IN 2OUTOUT4 FILOIN 1IN 2OUTOUT5 FIFOIN 1IN 2OUTOUTOUT5 FILOIN 1IN 2OUTIN 3OUT |
Sample Output122112None23 |
|
Source2007 provincial training team exercises (1) |
Recommendlcy |
Question Analysis:
Basic use of stack and queue, simple question. In fact, the speaker may mean that we can write a stack and queue by ourselves. However, as a slag that has long known STL, it is no longer patient to write stack and queue... Alas ..
The Code is as follows:
/**. Cpp * stack and queue simulation ** Created on: March 19, 2015 * Author: administrator */# include <iostream> # include <cstdio> # include <stack> # include <queue> using namespace std; int main () {int t; scanf ("% d", & t); while (t --) {int n; string type; cin >>n> type; if (type = "FIFO ") {queue <int> q; string cmd; int num; int I; for (I = 0; I <n; ++ I) {cin> cmd; if (cmd = "IN") {cin> num; q. push (num);} else {if (q. empty () = true) {printf ("None \ n");} else {int ans = q. front (); q. pop (); printf ("% d \ n", ans) ;}}} else {stack <int> st; string cmd; int num; int I; for (I = 0; I <n; ++ I) {cin> cmd; if (cmd = "IN") {cin> num; st. push (num);} else {if (st. empty () = true) {printf ("None \ n");} else {int ans = st. top (); st. pop (); printf ("% d \ n", ans) ;}}} return 0 ;}