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;
    }
  • 相关阅读:
    Mac保留Python2安装Python3(Anaconda3)
    Mybatis Plugin 以及Druid Filer 改写SQL
    Jackson替换fastjson
    Java单元测试 Http Server Mock框架选型
    应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值
    RabbitMQ Policy的使用
    Collectors.toMap不允许Null Value导致NPE
    php 删除标签
    php 替换标签
    jquery拼接html
  • 原文地址:https://www.cnblogs.com/candycloud/p/3399338.html
Copyright © 2011-2022 走看看