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)
上一篇 2022年2月5日 下午11:00
下一篇 2022年2月6日 上午6:00


相关推荐

  • 从零开始学习n8n-一文读懂n8n

    从零开始学习n8n-一文读懂n8n

    2026年3月13日
    2
  • nand flash基础时序

    nand flash基础时序一 综述 nandflash 的 8 个 I O IO0 IO7 在 NV DDR NV DDR2 andNV DDR3 规范里面又叫做 DQ0 DQ7 是复用的 也就是说可以传数据 也可以传地址 也可以传命令 为了进行区分 引入了 CLE CommandLatch ALE AddressLatch 两个管脚 CLE ALE DQ 数据总线上的内容含义 高电平 低电平 命令 低电平 高电平 地址 低电平 低电平 数

    2026年3月26日
    2
  • pycharm中创建虚拟环境「建议收藏」

    pycharm中创建虚拟环境「建议收藏」1什么是虚拟环境虚拟环境是用于依赖项管理和项目隔离的Python工具,允许Python站点包(第三方库)安装在本地特定项目的隔离目录中,而不是全局安装(即作为系统范围内的Python的一部分)。这听起来不错,但到底什么是虚拟环境呢?虚拟环境只是一个包含三个重要组件的目录:·安装了第三方库的site-packages/文件夹。·系统上安装的Python可执行文件的symlink符号链接。·确保执行Python代码的脚本使用在给定虚拟环境中安装的Python解释器和站点包。2.为什么使用虚

    2022年8月29日
    4
  • c语言用命令行打开文件_c语言无法打开文件

    c语言用命令行打开文件_c语言无法打开文件linux文件操作(打开及关闭)Linux文件描述符简介当一个进程获取文件的访问权时,通常指打开一个文件时,内核返回一个文件描述符,进程可以通过文件描述符进行后续的操作。文件描述符是一组正整数,每一个文件被打开时,内核都会打开一个大于或等于0的文件描述符。文件描述符012这是linux系统保留的三个文件描述符。0代表标准输入stdin1代表标准输出stdout2代表错误输出s…

    2025年6月17日
    4
  • CSS Reset(样式重置)

    CSS Reset(样式重置)CSSReset,意为重置默认样式。HTML中绝大部分标签元素在网页显示中都有一个默认属性值,通常为了避免重复定义元素样式,需要进行重置默认样式(CSSReset)。举几个例子:1.淘宝(CSS

    2022年7月2日
    24
  • Linux安装RabbitMQ详细教程

    Linux安装RabbitMQ详细教程目录一 准备环境准备 1 RabbitMQ 版本和 Erlang 版本兼容性关系 2 官方安装包下载地址 3 安装包中说明 请下载对应的安装包二 安装操作步骤 1 安装 C 依赖环境 2 准备安装包 3 在 opt 路径下 创建 rabbitmq 文件夹 4 将安装包上传到 rabbitmq 文件夹下 5 安装 Erlang6 检查 Erlang 是否安装成功 7 安装 socat8 安装 rabbitmq9 开启管理界面 10 添加配置文件 解决只能 localhost 访问的

    2026年3月19日
    2

发表回复

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

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