ACboy needs your help again!
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1697 Accepted Submission(s): 906
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
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
Source
Recommend
lcy
code1:栈和队列的使用
1 #include<iostream> 2 #include<stack> 3 #include<queue> 4 using namespace std; 5 int main() 6 { 7 char str[12]; 8 int n,m,cases; 9 scanf("%d",&n); 10 while(n--) 11 { 12 scanf("%d%str",&cases,str); 13 if(str[2]=='F') //队列 14 { 15 queue<int>Que; //创建一个int型队列 16 while(cases--) 17 { 18 scanf("%s",str); 19 if(str[0]=='I') 20 { 21 scanf("%d",&m); 22 Que.push(m); //入队 23 } 24 else if(!Que.empty()) 25 { 26 printf("%d\n",Que.front()); 27 Que.pop(); //出队 28 } 29 else 30 printf("None\n"); 31 } 32 } 33 else 34 { 35 stack<int>Sta; //创建一个int型的栈 36 while(cases--) 37 { 38 scanf("%s",str); 39 if(str[0]=='I') 40 { 41 scanf("%d",&m); 42 Sta.push(m); 43 } 44 else if(!Sta.empty()) 45 { 46 printf("%d\n",Sta.top()); 47 Sta.pop(); 48 } 49 else 50 printf("None\n"); 51 } 52 } 53 } 54 return 0; 55 }
code2:模拟
1 #include<iostream> 2 using namespace std; 3 4 int data[10002]; 5 char str[5]; 6 char ord[5]; 7 8 int main() 9 { 10 int n; 11 int i; 12 int cases; 13 int cnt; 14 int num; 15 int front; 16 while(~scanf("%d",&n)) 17 { 18 while(n--) 19 { 20 memset(data,0,sizeof(data)); 21 scanf("%d%s",&cases,str); 22 if(str[2]=='L') 23 { 24 cnt=0; 25 for(i=0;i<cases;i++) 26 { 27 scanf("%s",ord); 28 if(ord[0]=='I') 29 { 30 scanf("%d",&num); 31 data[cnt]=num; 32 cnt++; 33 } 34 if(ord[0]=='O') 35 { 36 if(cnt>0) 37 { 38 printf("%d\n",data[cnt-1]); 39 cnt--; 40 } 41 else 42 printf("None\n"); 43 } 44 } 45 } 46 else 47 { 48 cnt=0; 49 front=0; 50 for(i=0;i<cases;i++) 51 { 52 scanf("%s",ord); 53 if(ord[0]=='I') 54 { 55 scanf("%d",&num); 56 data[cnt]=num; 57 cnt++; 58 } 59 if(ord[0]=='O') 60 { 61 if(front<cnt) 62 { 63 printf("%d\n",data[front]); 64 front++; 65 } 66 else 67 printf("None\n"); 68 } 69 } 70 } 71 } 72 } 73 return 0; 74 } 75 /* 76 4 77 7 FIFO 78 IN 1 79 IN 3 80 IN 5 81 OUT 82 OUT 83 OUT 84 OUT 85 */