Web Navigation Time limit: 2 Seconds Memory Limit: 65536 KB
Standard Web browsers contain features to move backward and forward among the pages recently visited. One-to-implement these features is-to-use-stacks-keep track of the pages, can be reached by moving backward and forward. This problem is asked to implement.
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 <url>: Push The current page to the top of the backward stack, and make the URL specified the new cur Rent page. The forward stack is emptied.
quit: quit the browser.
Assume the browser initially loads the Web page at the URL http://www.acm.org/
This problem contains multiple test cases!
The first line of a multiple input was an integer N and then a blank line followed by N input blocks. Each input block was in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Input
Input is a sequence of commands. The command keywords back, FORWARD, VISIT, and QUIT is all in uppercase. URLs have no whitespace and are at the most characters. You may assume this no problem instance requires more than the elements in each stack at any time. The end of input is indicated by the QUIT command.
Output
For each command and other than QUIT, print the URL of the current page after the command is executed if the command was not IG Nored. Otherwise, print "ignored". The output for each command should is printed on their own line. No output is produced for the QUIT command.
Sample Input
1
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
Use two stacks according to the topic.
The stack in C + + does not have a clear function, which is used to reconstruct a memory leak, but it has no effect on the problem
1#include <iostream>2#include <cmath>3#include <cstdio>4#include <vector>5#include <list>6#include <string>7#include <cstring>8#include <cstdio>9#include <algorithm>Ten#include <Set> One#include <stack> A - using namespacestd; - the intMain () - { - intN; -CIN >>N; + Cin.ignore (); - + while(n--) A { at stringcmd; - Cin.ignore (); - -stack<string>forward, back; -Back.push ("http://www.acm.org/"); - in while(Cin >> cmd && cmd! ="QUIT") - { to if(cmd = ="VISIT") + { - stringURL; theCIN >>URL; *Forward = stack<string>(); $ back.push (URL);Panax Notoginsengcout << back.top () <<Endl; - } the Else if(cmd = =" Back") + { A if(Back.size () >1) the { + Forward.push (Back.top ()); - Back.pop (); $cout << back.top () <<Endl; $ } - Else -cout <<"ignored"<<Endl; the } - ElseWuyi { the if(!forward.empty ()) - { Wu Back.push (Forward.top ()); - Forward.pop (); Aboutcout << back.top () <<Endl; $ } - Else -cout <<"ignored"<<Endl; - } A } + if(N >0) thecout <<Endl; - } $}
Zoj 1061 Web Navigation