zoukankan      html  css  js  c++  java
  • PROJ1028

    Web Navigation
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 27086   Accepted: 12109

    Description

    Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you 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 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 that 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 are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is 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 the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.

    Sample Input

    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

     
    #include<iostream>
    #include<string>
    using namespace std;
    
    struct node{
    	node *next;
    	char *url;
    	node(){
    		next = NULL;
    	}
    };
    
    void push(node *top,char *url){
    	node *new_node = new node;
    	new_node->next = top->next;
    	new_node->url = url;
    	top->next = new_node;
    }
    
    void push1(node *top,char *url){
    	node *new_node = new node;
    	//new_node->next = top->next;
    	new_node->url = url;
    	top->next->next = new_node;
    	top->next = new_node;
    }
    
    char *pop(node *top){
    	if(top->next == NULL){
    		return "Ignored";
    	}else{
    		char *s = top->next->url;
    		top->next = top->next->next;
    		return s;
    	}
    }
    
    bool equal(char *a, char *b){
    	int i = 0;
    	while((a[i] == b[i])&& (a[i] != '' &&b[i] != '')){
    		++i;
    	}
    	if(a[i] == '' && b[i] == '')return true;
    	return false;
    }
    
    int main()
    {
    	node *top1 = new node,*top2 = new node;
    	node *result = new node, *position  = new node;
    	position->next = result;
    	char *now = "http://www.acm.org/";
    	char *order = new char[10];
    
    	cin>>order;
    	while(!equal(order, "QUIT")){
    		if(equal(order, "VISIT")){
    			char *the_url = new char[101]; 
    			cin>>the_url;
    			push(top1,now);
    			now = the_url;
    			
    			push1(position,now);
    			top2->next = NULL;
    		}else if(equal(order, "BACK")){
    			if(top1->next == NULL){
    				char *s = pop(top1);
    				
    				push1(position,s);
    			}else{
    				push(top2,now);
    				now = pop(top1);
    				
    				push1(position,now);
    			}
    		}else if(equal(order, "FORWARD")){
    			if(top2->next == NULL){
    				char *s = pop(top2);
    				
    				push1(position,s);
    			}else{
    				push(top1,now);
    				now = pop(top2);
    				
    				push1(position,now);
    			}
    		}
    		cin>>order;
    	}
    	while(result->next != NULL){
    		cout<<result->next->url<<endl;
    		result->next = result->next->next;
    	}
    
    	return 0;
    }
  • 相关阅读:
    解决Firefox下outerHTML不支持问题
    神奇的css属性pointerevents
    IE6 double marginleft Bug
    解决IE低版本不支持call和apply问题
    JavaScript函数参数的可修改性
    IE6/7 double paddingbottom Bug
    各浏览器对document.getElementById等方法的实现差异
    JavaScript中两种类型的全局对象/函数
    JavaScript子类用Object.getPrototypeOf去调用父类方法
    JavaScript声明全局变量三种方式的异同
  • 原文地址:https://www.cnblogs.com/candycloud/p/3399338.html
Copyright © 2011-2022 走看看