Web Navigation poj 1028 模拟

来源:互联网 发布:会计软件 免费 编辑:程序博客网 时间:2024/06/05 17:52

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.

分析

水模拟,但是看到老伙计写了,我也忍不住去写一发
两个栈搞定

代码

var  a,b:array[1..2001] of ansistring;  top1,top2:longint;  s:ansistring;  s1:ansistring;  i,j,k:longint;begin  s:='http://www.acm.org/';  top1:=0;  top2:=0;  readln(s1);  while s1<>'QUIT' do    begin      if s1[1]='V'        then begin          top1:=top1+1; a[top1]:=s;          top2:=0;          s:=copy(s1,7,length(s1)-6);          writeln(s);          readln(s1);          continue;        end;      if s1[1]='B'        then begin          if top1=0            then begin              writeln('Ignored');              readln(s1);              continue;            end;          top2:=top2+1;          b[top2]:=s;          s:=a[top1];          top1:=top1-1;          writeln(s);          readln(s1);          continue;        end;      if s1[1]='F'        then begin          if top2=0            then begin              writeln('Ignored');              readln(s1);              continue;            end;          top1:=top1+1;          a[top1]:=s;          s:=b[top2];          top2:=top2-1;          writeln(s);          readln(s1);          continue;        end;    end;  close(input);  close(output);end.
0 0
原创粉丝点击