Problem Description
Problem Analysis
The problem provides two modes
FIFO: first in, first out
FILO: first in, last out
They correspond to a queue and a stack respectively
(a queue is first in, first out; a stack is first in, last out)
So just simulate them directly
One thing to pay special attention to is that, for the two structures,
the memory of each command must be accurate
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
| #include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std;
stack<int>a;
queue<int>q;
int main() {
int T;
scanf("%d",&T);
while(T--) {
int n;
char s[10];
scanf("%d %s",&n,s);
if(strcmp(s,"FIFO")==0) {
while(n--) {
char st[10];
scanf("%s",st);
if(strcmp(st,"IN")==0) {
int t;
scanf("%d",&t);
q.push(t);
} else {
if(q.empty())
printf("None\n");
else {
printf("%d\n",q.front());
q.pop();
}
}
}
} else {
while(n--) {
char st[10];
scanf("%s",st);
if(strcmp(st,"IN")==0) {
int t;
scanf("%d",&t);
a.push(t);
} else {
if(a.empty())
printf("None\n");
else {
printf("%d\n",a.top());
a.pop();
}
}
}
}
while(!a.empty())
a.pop();
while(!q.empty())
q.pop();
}
return 0;
}
|
Problem address: HDU 1702 ACboy needs your help again!