poj 1028 Web Navigation(模拟)「建议收藏」

poj 1028 Web Navigation(模拟)

大家好,又见面了,我是全栈君。

题目链接:http://poj.org/problem?

id=1028

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 <algorithm>
#include <string>
#include <stack>
using namespace std;
stack <string>b;
stack <string>f;
int main()
{
    string tt = "http://www.acm.org/";
    string s;
    while(cin >> s)
    {
        if(s == "QUIT")
            break;
        if(s == "VISIT")
        {
            b.push(tt);
            cin >> tt;
            cout<<tt<<endl;//始终输出当前页
            while(!f.empty())//当新訪问一个页面的时候把之前页面前面的清空
            {
                f.pop();
            }
        }
        else if(s == "BACK")
        {
            if(!b.empty())
            {
                f.push(tt);
                tt = b.top();
                b.pop();
                cout<<tt<<endl;//始终输出当前页
            }
            else
                cout<<"Ignored"<<endl;
        }
        else
        {
            if(!f.empty())
            {
                b.push(tt);
                tt = f.top();
                f.pop();
                cout<<tt<<endl;//始终输出当前页
            }
            else
                cout<<"Ignored"<<endl;
        }
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115330.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电脑如何进行截图识别

    电脑如何进行截图识别使用电脑的时候,我们经常会用到很多快捷键,就比如说截图,当我们把需要的文字内容截图之后,如何才能顺手把截图中的文字转换成word文字呢,下面小编分享一个简单的方法,在把文字内容截图之后直接可以转换成文字的方法。使用工具:迅捷OCR文字识别软件;操作方法:第一步:大家可以通过浏览器搜索关键词“OCR文字识别软件”找到该文字识别软件;第二步:在电脑上运行文字识别软件,点击左边功能栏中的“截图识别”功…

    2022年6月5日
    78
  • 关于Java你不知道的那些事之Java注解和反射

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 作者:轻狂书生FS https://blog.csdn.net/LookForDream_/ 前言 我们在实际的工作…

    2021年6月27日
    125
  • vuex使用教程(最好最详细的乒乓教程)

    最详细的Vuex教程什么是Vuex?vuex是一个专门为vue.js设计的集中式状态管理架构。状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态。简单的说就是data中需要共用的属性。引入Vuex(前提是已经用Vue脚手架工具构建好项目)1、利用npm包管理工具,进行安装vuex。在控制命令行中输入下边的命令就可以了。npminstallvuex

    2022年4月14日
    116
  • 网站的引导页介绍

    外贸企业站就是要走出过门,买域名,买空间的、请设计公司、请这请那,一切的努力都是要为了能通过网站给公司带来订单,“一分耕耘一分收获”,但是耕耘了未必就有收获,很多企业站点建好了,可是并没有发现,它所给

    2021年12月25日
    40
  • vscode代码自动提示_vscode智能提示

    vscode代码自动提示_vscode智能提示VSCode自动补全,VSCode当中的自动补全内容,其实是由语言服务来提供的。本文介绍VSCode自动补全功能和VSCode自动补全设置。VSCode为编程语言工作者提供了统一的API,即LanguageServerProtocol,每种语言都能够通过实现这个API在VSCode上得到类似IDE的开发体验,而各个语言根据这个API实现的服务,就被称为语言…

    2025年8月19日
    4
  • springboot的介绍_springboot官网

    springboot的介绍_springboot官网Springboot入门介绍一、Spring框架概述1.1什么是SpringSpring是一个开源框架,Spring是于2003年兴起的一个轻量级的Java开发框架,由RodJohnson在其著作《ExpertOne-On-OneJ2EEDevelopmentandDesign》。Spring是为了解决企业级应用开发的复杂性而创建的,使用Spring可以让简单的Ja…

    2022年9月23日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号