Acboy was kidnapped!!
He miss his mother very much and was very scare now. You can "t image how dark the" the "the" the "and" put into are, so poor:(.
As a smart Acmer, you want to get Acboy out of the monster ' s Labyrinth. If you arrive at the gate of the maze, the Monste say: "I had heard that is very clever, but if can ' t solve M Y problems, you'll 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 is very happy because you know "FIFO" stands for "first on first out", and "FILO" means "first on last Out").
And the following N lines, each of which is ' in M ' or ' out ', (M represent a integer).
And the answer of a problem are 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.
The input of each subproblem is described above.
Outputfor each command ' out ', you should output an integer depend on the word was "FIFO" or "FILO", or a word "None" if you Don ' t has any integer.
Sample Input4 4 FIFO in 1-2 out-out 4 FILO in 1 in 2 out-out 5-FIFO in 1 in 2 out-out 5 FILO in 1 in 2 out in 3 out
Sample OUTPUT1 2 2 1 1 2 None 2 3
queues and stacks: #include<stdio.h>#include<string.h>#include<queue>#include<stack># define NTenusing namespacestd;intMain () {intt, n, x, T; CharS[n], str[n]; scanf ("%d", &T); while(t--) {scanf ("%d%s", &n, str); if(strcmp (str,"FIFO") ==0) {Queue<int>Q; while(n--) {scanf ("%s", s); if(strcmp (s),"inch") ==0) {scanf ("%d", &x); Q.push (x); } Else { if(!Q.empty ()) {T=Q.front (); Q.pop (); printf ("%d\n", T); } Elseprintf ("none\n"); } } } //FIFO stands for FIFO, use queue to do Else{Stack<int>Q; while(n--) {scanf ("%s", s); if(strcmp (s),"inch") ==0) {scanf ("%d", &x); Q.push (x); } Else { if(!Q.empty ()) {T=Q.top (); Q.pop (); printf ("%d\n", T); } Elseprintf ("none\n"); } } } //Filo represents advanced and out, using stacks to do } return 0;}
queue: #include<stdio.h>#include<queue>using namespacestd;intMain () {inta[5] = {1,2,4,5,6}, I, X; Queue<int>Q; for(i =0; I <5; i++) Q.push (A[i]); while(!Q.empty ()) {x=Q.front (); Q.pop (); printf ("%d", x); } printf ("\ n"); return 0;} Priority queue: #include<stdio.h>#include<queue>using namespaceStd;typedefstructnode{intx, step; FriendBOOL operator<(node n1, node N2) {returnN1.step >N2.step; }}node;intMain () {NODE no[5] = { {1,5}, {2,2}, {3,4}, {4,3}, {1,1}}, N; inti; Priority_queue<NODE>Q; for(i =0; I <5; i++) Q.push (No[i]); while(!Q.empty ()) {N=Q.top (); Q.pop (); printf ("%d%d\n", n.x, n.step); } return 0;} Stack: #include<stdio.h>#include<stack>using namespacestd;intMain () {inta[5] = {1,3,4,5,6}, I, X; Stack<int>Q; for(i =0; I <5; i++) Q.push (A[i]); while(!Q.empty ()) {x=Q.top (); Q.pop (); printf ("%d", x); } printf ("\ n"); return 0;}
View Code
HDU 1702 Acboy needs your help again! (additional priority queue)