POJ 1028 Web Navigation (simulation method)

Source: Internet
Author: User

Web Navigation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 29819 Accepted: 13328

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. 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: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 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 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

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

Test Instructions Brief Description:
This is a simulation of the Web browser program, the default home page for the http://www.acm.org/input as a directive, the book is the current page URL.
There are 4 types of input directives:
(1) VISIT (followed by a URL)--access the URL immediately following it.
(2) back--access the previous one of the current page.
(3) forward--access the latter of the current page.
(4) quit--exit (Close browser).

Ideas:
The visited URLs must be recorded because they are sequential, so the element with the index 0 is the default page, and a variable records the position of the current page in the array; the back and forward directives may have special circumstances:
(1) Back to the default page and then back--output ignored.
(2) forward to the last visited page again with forward--output ignored.
In this way, there is a problem: to record the last access to the page, for this problem only need to encounter visit instructions when the operation of the instructions above the corresponding operation has become very simple.
visit--writes the URL to an array and then outputs it, recording the current position, which is the last page visited.
back--current page minus 1, output URL, pay attention to judge special circumstances.
forward--Current page +1, output URL, also pay attention to special circumstances.
quit--exit.
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <string.h>5 using namespacestd;6 intCheckcommand (Char*command)7 {8     if(strcmp (Command,"VISIT")==0)9         return 0;Ten     if(strcmp (Command," Back")==0) One         return 1; A     if(strcmp (Command,"FORWARD")==0) -         return 2; -     if(strcmp (Command,"QUIT")==0) the         return 3; - } - intMain () - { +     Charbrowser[101][ in]={"http://www.acm.org/"}; -     Charcommand[Ten]; +     intI=0; A     intCur=0; at      while(1) -     { -Cin>>command; -         intn=checkcommand (command); -         Switch(n) -         { in              Case 0: -i++; toCur=i; +Cin>>Browser[i]; -cout<<browser[i]<<Endl; the                  Break; *  $              Case 1:Panax Notoginsengi--; -                 if(i>=0) the                 { +cout<<browser[i]<<Endl; A                 } the                 Else +                 { -cout<<"ignored"<<Endl; $i++; $                 } -                  Break; -              Case 2: thei++; -                 if(i<=cur)Wuyi                 { thecout<<browser[i]<<Endl; -                 } Wu                 Else -                 { Aboutcout<<"ignored"<<Endl; $i--; -                 } -                  Break; -              Case 3: A                 return 0; +         } the     } -     return 0; $}

POJ 1028 Web Navigation (simulation method)

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.