问题描述
问题分析
题目给出两种形式
FIFO先进先出
FILO先进后出
分别对应队列和栈
(队列先进先出,栈先进后出)
所以直接模拟就好了
特别注意的就是对于两种结构
各个命令的记忆需要准确
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;
}
|
题目地址:【杭电】[1702]ACboy needs your help again!