Discover the Web (stack emulation)

Source: Internet
Author: User

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:

    1. 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.
    2. 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.
    3. 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.
    4. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.