zoukankan      html  css  js  c++  java
  • # 栈 ——Web Navigation POJ

    原文

    Web Navigation POJ - 1028 
    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
    

    翻译

    网络导航 POJ-1028 
    标准的Web浏览器包含在最近访问的页面之间前进和后退的功能。实现这些功能的一种方法是使用两个堆栈来跟踪通过向前和向后移动可以到达的页面。在此问题中,要求您实施此操作。 
    需要支持以下命令: 
    BACK:将当前页面推入正向堆栈的顶部。从向后堆栈的顶部弹出页面,使其成为新的当前页面。如果向后堆栈为空,则忽略该命令。 
    前进:将当前页面推入后退堆栈的顶部。从向前堆栈的顶部弹出页面,使其成为新的当前页面。如果前向堆栈为空,则忽略该命令。 
    访问 :将当前页面推入向后堆栈的顶部,并使指定的URL为新的当前页面。前向堆栈为空。 
    退出:退出浏览器。 
    假设浏览器最初是通过URL http://www.acm.org/加载网页的。
    输入项
    输入是一系列命令。命令关键字BACK,FORWARD,VISIT和QUIT均为大写。URL没有空格,最多70个字符。您可以假设没有问题实例在任何时候在每个堆栈中都不需要超过100个元素。输入的结束由QUIT命令指示。
    输出量
    对于QUIT以外的每个命令,如果不忽略该命令,则在执行该命令后打印当前页面的URL。否则,打印“已忽略”。每个命令的输出应打印在自己的行上。QUIT命令不产生任何输出。
    样本输入
    访问http://acm.ashland.edu/
    访问http://acm.baylor.edu/acmicpc/
    背部
    背部
    背部
    向前
    访问http://www.ibm.com/
    背部
    背部
    向前
    向前
    向前
    退出
    样本输出
    http://acm.ashland.edu/
    http://acm.baylor.edu/acmicpc/
    http://acm.ashland.edu/
    http://www.acm.org/
    被忽略
    http://acm.ashland.edu/
    http://www.ibm.com/
    http://acm.ashland.edu/
    http://www.acm.org/
    http://acm.ashland.edu/
    http://www.ibm.com/
    被忽略
    

    思路注释&答案

    #include<iostream>
    #include<cstdio>
    #include<stack>
    #include<string>
    using namespace std;
    int main(){
    	string str1,str2;
    	stack<string>backst,forwst;
    	backst.push("http://www.acm.org/");//初始网站
    	while(cin>>str1){
    			if(str1=="BACK"){//“返回”操作
    				if(backst.size()==1)cout<<"Ignored"<<endl;//没有可返回的网站时输出“无效”
    				else{//如果有可以返回的网站时
    					forwst.push(backst.top());//先把当前网站加入 “可前进”的栈
    					backst.pop();//出掉这个网站
    					cout<<backst.top()<<endl;//输出返回到的这个网站
    				}
    			}
    			else if(str1=="FORWARD"){//前进操作
    				if(forwst.size()==0){//没得前进时
    					cout<<"Ignored"<<endl;
    				}else{//有得前进时
    					backst.push(forwst.top());//现将要前进到的网站放回可返回的网站的栈里
    					cout<<forwst.top()<<endl;//输出前进到的栈
    					forwst.pop();//然后出栈
    				}
    			}
    			else if(str1=="VISIT"){//打开链接操作
    				cin>>str2;//输入链接
    				backst.push(str2);//链接入栈
    				cout<<backst.top()<<endl;//输出链接
    				while(!forwst.empty())//然后把所有可前进的栈都出来
    					forwst.pop();
    			}else if(str1=="QUIT"){
    				break;
    			}
    	}
    	return 0;
    }```
    
    
  • 相关阅读:

    bzoj3052: [wc2013]糖果公园
    莫队算法心得
    bzoj1104: [POI2007]洪水pow
    bzoj1102: [POI2007]山峰和山谷Grz
    bzoj1121: [POI2008]激光发射器SZK
    bzoj1113: [Poi2008]海报PLA
    bzoj1103: [POI2007]大都市meg
    bzoj1396: 识别子串
    bzoj3756: Pty的字符串
  • 原文地址:https://www.cnblogs.com/yuanyulin/p/14026798.html
Copyright © 2011-2022 走看看