Description
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. You is asked to implement this. The commands are:
- Back:if The backward stack is empty and the command is ignored. Otherwise, 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.
- Forward:if the FORWARD stack is empty and the command is ignored. Otherwise, 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.
- VISIT <url>: Push The current page to the top of the backward stack, and make the URL to specified the new current page . The forward stack is emptied.
- Quit:quit the browser.
The browser initially loads the Web page at the URL ' http://www.lightoj.com/'
Input
Input starts with an integer T (≤100), denoting the number of test cases.
Each case contains some commands. The keywords back, FORWARD, VISIT, and QUIT is all in uppercase. URLs have no whitespace and are at the most characters. The end of case was indicated by the QUIT command and it shouldn ' t was processed. Each case is contains at the most lines.
Output
For each case, print the case number first. For each command, print the URL of the ' current page ' after the command was executed if the command is not ignore D. Otherwise, print ' ignored '.
Sample Input
1
VISIT http://uva.onlinejudge.org/
VISIT http://topcoder.com/
Back
Back
Back
FORWARD
VISIT http://acm.sgu.ru/
Back
Back
FORWARD
FORWARD
FORWARD
QUIT
Sample Output
Case 1:
http://uva.onlinejudge.org/
http://topcoder.com/
http://uva.onlinejudge.org/
http://www.lightoj.com/
Ignored
http://uva.onlinejudge.org/
http://acm.sgu.ru/
http://uva.onlinejudge.org/
http://www.lightoj.com/
http://uva.onlinejudge.org/
http://acm.sgu.ru/
Ignored
The process of using a stack to simulate a browser's access to a Web page.
problem-solving ideas: build two stacks, respectively, to store forward and backward elements, in fact, is falling back and forth.
Alas, for a long time did not do the problem, ran into this stack of questions, back and forth to do a long time, but also in the loopholes before the patch, had encountered such a double stack of the topic, but did not fill the problem in time, dragged to the present.
1#include <iostream>2#include <stack>3#include <algorithm>4#include <stdio.h>5 using namespacestd;6 intMain ()7 {8 intT,count=1;9 stringx, y;Tenscanf"%d",&t); One while(t--) A { -printf"Case %d:\n", count++); -stack<string>S1; thestack<string>S2; -S1.push ("http://www.lightoj.com/"); - while(1) - { +Cin>>x; - if(x[0]=='Q') + { A Break; at } - Else if(x[0]=='V') - { -Cin>>y; - S1.push (y); -cout<<y<<Endl; in while(!s2.empty ())///Clear - { to S2.pop (); + } - } the Else if(x[0]=='B')///because the top element of the S1 stack is the currently visited page, a step back must return the next element at the top of the current stack. * { $ if(S1.size () >1)/// must have more than two elements in the stack at this timePanax Notoginseng { - S2.push (S1.top ()); theS1.pop ();///Delete the current page +Cout<<s1.top () <<Endl; A } the Else + { -cout<<"ignored"<<Endl; $ } $ } - Else if(x[0]=='F') - { the if(!s2.empty ()) - {Wuyi S1.push (S2.top ()); theCout<<s2.top () <<Endl; -S2.pop ();///Delete the current page Wu } - Else About { $cout<<"ignored"<<Endl; - } - } - } A } + return 0; the}
Discover the Web (stack emulation)