Poj 1028 Web navigation
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:17718 |
|
Accepted:7751 |
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 shoshould 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/BACKBACKBACKFORWARDVISIT http://www.ibm.com/BACKBACKFORWARDFORWARDFORWARDQUIT
Sample output
http://acm.ashland.edu/http://acm.baylor.edu/acmicpc/http://acm.ashland.edu/http://www.acm.org/Ignoredhttp://acm.ashland.edu/http://www.ibm.com/http://acm.ashland.edu/http://www.acm.org/http://acm.ashland.edu/http://www.ibm.com/Ignored
Source
East central North America 2001my AC code # Pragma warning (Disable: 4786) <br/> # include <iostream> <br/> # include <stack> <br/> # include <string> <br/> using namespace STD; <br/> // note that the currently accessed page is the element at the top of the backset stack, remember to <br/> // understand the meaning of the browser's forward and backward: <br/> int main () {<br/> string Brower; <br/> stack <string> backst, Forst; <br/> string command, URL; <br/> backst. push ("http://www.acm.org/"); <br/> while (CIN> command, command! = "Quit") <br/>{< br/> If (command = "visit") <br/>{< br/> CIN> URL; <br/> backst. push (URL); <br/> cout <URL <Endl; <br/> while (! Forst. empty () Forst. pop (); <br/>}< br/> else if (command = "back") <br/>{< br/> If (backst. size () = 1) // equal to 1 indicates the current page, the stack cannot be played. <br/>{< br/> cout <"ignored" <Endl; <br/>}< br/> else <br/>{< br/> url = backst. top (); <br/> Forst. push (URL); <br/> backst. pop (); <br/> url = backst. top (); <br/> cout <URL <Endl; <br/>}</P> <p >}< br/> else if (command = "Forward") <br/>{< br/> If (Forst. size () = 0) // The Forst size can be zero, which must be distinguished from the backset <br/>{< br/> cout <"ignored" <Endl; <br/>}< br/> else <br/> {<br/> url = Forst. top (); <br/> backst. push (URL); <br/> Forst. pop (); <br/> cout <URL <Endl; <br/>}< br/> return 0; <br/>}< br/>