POJ 1028 Web Navigation (模拟法)

Web Navigation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29819   Accepted: 13328

Description

Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this.
The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/

Input

Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.

Output

For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.

Sample Input

VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

Sample Output

http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored

题意简述:
  这题是一个模拟WEB浏览器的程序,默认主页为http://www.acm.org/输入为指令,出书为当前页面的网址。
输入的指令有4种:
  (1)VISIT(后接一个网址)——访问它后面紧接着的网址。
  (2)BACK——访问当前网页的前一个。
  (3)FORWARD——访问当前网页的后一个。
  (4)QUIT——退出(关闭浏览器)。

思路:
  必须记录访问过的网址,因为是顺序关系,所以用数组存储,下标为0的元素为默认页;有一个变量记录当前页面在数组中的位置;BACK和FORWARD指令可能出现特殊情况:
  (1)BACK到默认页面再BACK——输出Ignored。
  (2)FORWARD到最后一个访问到的页面再与FORWARD——输出Ignored。
  这样,就有一个问题:要记录最后一个访问到的页面,对于这个问题只需要在遇到VISIT指令的时候顺便操作上面的指令的对应操作就变得很简单。
  VISIT——就将网址写入数组后再进行输出,记录当前位置,这个位置也即最后一个访问到的页面。
  BACK——当前页面减1,输出网址,注意判断特殊情况。
  FORWARD——当前页面+1,输出网址,也要注意特殊情况。
  QUIT——退出。
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string.h>
 5 using namespace std;
 6 int checkCommand(char* command)
 7 {
 8     if(strcmp(command,"VISIT")==0)
 9         return 0;
10     if(strcmp(command,"BACK")==0)
11         return 1;
12     if(strcmp(command,"FORWARD")==0)
13         return 2;
14     if(strcmp(command,"QUIT")==0)
15         return 3;
16 }
17 int main()
18 {
19     char browser[101][71]={"http://www.acm.org/"};
20     char command[10];
21     int i=0;
22     int cur=0;
23     while(1)
24     {
25         cin>>command;
26         int n=checkCommand(command);
27         switch(n)
28         {
29             case 0:
30                 i++;
31                 cur=i;
32                 cin>>browser[i];
33                 cout<<browser[i]<<endl;
34                 break;
35 
36             case 1:
37                 i--;
38                 if(i>=0)
39                 {
40                     cout<<browser[i]<<endl;
41                 }
42                 else
43                 {
44                     cout<<"Ignored"<<endl;
45                     i++;
46                 }
47                 break;
48             case 2:
49                 i++;
50                 if(i<=cur)
51                 {
52                     cout<<browser[i]<<endl;
53                 }
54                 else
55                 {
56                     cout<<"Ignored"<<endl;
57                     i--;
58                 }
59                 break;
60             case 3:
61                 return 0;
62         }
63     }
64     return 0;
65 }
原文地址:https://www.cnblogs.com/caterpillarofharvard/p/4231475.html