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;
    }
  • 相关阅读:
    Kafka(Go)系列(一)通过dockercompose 安装 Kafka
    grpc通过自签CA证书、server、client双向认证【支持go1.15】
    Go 基准测试和性能测试学习使用
    Debian关闭防火墙
    服务依赖开闭简单算法
    Hbase 和 Hive 的区别,各自使用场景。
    VMware虚拟机 Ubuntu1404
    安装protoc
    Redis大 key 自动过期的问题
    MySQL设置数据库为只读
  • 原文地址:https://www.cnblogs.com/candycloud/p/3399338.html
Copyright © 2011-2022 走看看