Zoj Problem set-1061_ Simulation problem

Source: Internet
Author: User

"Simulation Questions"

Topic

Web navigation time limit:2 Seconds Memory limit:65536 MB Standard Web browsers contain features to move backward And forward among the pages recently visited. One way to implement these features are to use two stacks to keep track of the pages this can be reached by moving backward and forward. In this problem, your 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 to 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 to the new current page. If The forward stack is empty, the command is ignored.

VISIT <url>: Push the "current page" on the "Backward stack" and "make" 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/


This problem contains multiple test cases!

The A multiple input is a integer N, then a blank line followed by n input blocks. Each input's 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

The 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 characters. You may assume this no problem instance requires more than-elements in each stack. The end of the input was 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 ' command is not IG Nored. Otherwise, print "ignored". The output for each command should is printed on it 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
Source:east North America 2001

"Instructions"

Basic Web page access in the browser generally supports three types of access: Enter the URL in the Address bar, back up, forward. Now to simulate a simple browser, program input includes a number of sets of use cases, each set of use cases to enter three types of access to the Web page operations: (1) direct access to the URL (VISIT URL) (2) for the current page for the back (3) for the current page forward (FORWARD), Each input for each set of use cases corresponds, outputs the URL of the Web page that is currently being accessed, output "ignored" if the error causes the Web page to be unreachable (for example, when you enter URL access, or when you go back to the first web page and still operate back access), and each set of use cases ends with "QUIT".

Troubleshooting

(a) Analysis: When you enter URL access or forward access, the last time you have just visited the URL of the Web page into a back stack, then the back stack of the stack to save the top relative to the current page before the most recently visited pages, so that when the back operation can be pop-up top page as a back operation is visiting the page; When you go back, you can push the URL of the page you just visited into a forward stack, the top of the forward stack saves the most recent Web page after the back-access page, so that when the forward operation is carried out, the top page of the stack can be ejected as the page being accessed by the forward operation, and it is not difficult to conclude that when a backward or forward operation is made, If the back stack or forward stack is empty, the Web page cannot be reached.



(ii) Code:

#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <stack

> Using namespace std;
	int main () {int n,num;
	Stack<string> Back,forward;
	String url,current;//current records the Web page currently being accessed (triggered by Visit/back/forward) Char action[10];
	scanf ("%d", &n);
		n use case for (num=1;num<=n;num++) {current= "http://www.acm.org/";
		Clear the back stack, forward stack while (!back.empty ()) Back.pop () before each use case;
		while (!forward.empty ()) Forward.pop (); 
			Enter Web page access action while (scanf ("%s", action) &&strcmp (action, "QUIT")!=0) {//url access mode if (strcmp (Action, "VISIT") ==0)
				{cin>>url;
				Back.push (current);//press the last visited page into the back stack standby current=url; cout<<current<<endl;//output the page currently being accessed//when the input URL accesses a page, the forward action has no web page, so empty the forward stack while (!forward.empty ()) Forwar
			D.pop (); }//Back mode else if (strcmp (Action, "==0") {if (!back.empty ()) {Forward.push (current), or the page you just visited
					Forward stack standby url=back.top (); current=url;//the currently visited Web page is a back stackThe top Back.pop ()///top of the stack deletes the original top cout<<current<<endl;//output the page currently being accessed} else printf ("ignored\n"); //forward mode else if (strcmp (Action, "FORWARD") ==0) {if (!forward.empty ()) {Back.push (current); will have just visited
					Ask the page to press into the back stack standby url=forward.top (); current=url;//The current access page is the forward stack top forward.pop ()///top of the stack, delete the original stack cout<<current<<endl;//output the page currently being accessed} ELS
			E printf ("ignored\n");
	} if (num<n) printf ("\ n");
return 0; }//accepted

(Solution to 2009/10)


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.