问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1028思路:
这是道简单题,1次AC呵呵
最简单的做法莫过于直接用两个堆栈根据题目的description进行模拟
不过,这里,我只用了一个数组来模拟所有的动作
需要注意的一点是: FORWARD的动作只有在之前存在BACK动作的条件下才有可能有效
1 #define LEN_MAX 71
2 #define NUM_MAX 100
3 #define CMD_MAX 8
4 #define QUIT "QUIT"
5 #define FORWARD "FORWARD"
6 #define VISIT "VISIT"
7 #define BACK "BACK"
8 #define IGN "Ignored"
9 char cmd[CMD_MAX];
10 char addr[LEN_MAX];
11 char arr[NUM_MAX][LEN_MAX];
12 int cur_pos, bk_pos, fw_pos;
13 int can_fw_count;
14
15 int
16 main(int argc, char **argv)
17 {
18 cur_pos = can_fw_count = 0;
19 bk_pos = fw_pos = -1;
20 strcpy(arr[cur_pos], "http://www.acm.org/");
21 while(scanf("%s", cmd)!=EOF && strcmp(cmd, QUIT)!=0) {
22 if(strcmp(cmd, VISIT)==0) {
23 scanf("%s", addr);
24 strcpy(arr[++cur_pos], addr);
25 bk_pos = cur_pos - 1;
26 can_fw_count = 0;
27 printf("%s\n", arr[cur_pos]);
28 } else if(strcmp(cmd, BACK)==0) {
29 if(bk_pos == -1)
30 printf("%s\n", IGN);
31 else {
32 fw_pos = cur_pos;
33 cur_pos = bk_pos;
34 bk_pos = cur_pos-1;
35 ++can_fw_count;
36 printf("%s\n", arr[cur_pos]);
37 }
38 } else if(strcmp(cmd, FORWARD)==0) {
39 if(fw_pos == -1 || !can_fw_count)
40 printf("%s\n", IGN);
41 else {
42 bk_pos = cur_pos;
43 cur_pos = fw_pos;
44 fw_pos = cur_pos+1;
45 --can_fw_count;
46 printf("%s\n", arr[cur_pos]);
47 }
48 }
49 }
50 return 0;
51 }