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


相关推荐

  • 汇编语言——移位指令[通俗易懂]

    基本概念移位操作指令:移位操作指令是一组经常使用的指令,属于汇编语言逻辑指令中的一部分,它包括移位指令(含算术移位指令、逻辑移位指令),循环移位指令(含带进位的循环移位指令),双精度移位指令三大类。其功能为将目的操作数的所有位按操作符规定的方式移动1位或按寄存器CL规定的次数(0~255)移动,结果送入目的地址。目的操作数是8位(或16位)的寄存器数据或存储器数据。基本格式格式为…

    2022年4月16日
    147
  • bWAPP 安装_bud在哪里下载

    bWAPP 安装_bud在哪里下载1.前言bwapp是一款非常好的漏洞演示平台,其包含有100多个漏洞。bwapp漏洞平台的安装大致有3种单独下载,部署到apache+mysql+php环境下直接下载虚拟机使用docker进行安装2.单独下载先去下载bwapp环境然后将下载的压缩包解压在bwapp/admin下找到settings.php文件将配置文件的内容修改为自己的内容然后访问bwapp进…

    2026年3月3日
    5
  • IntelliJ IDEA 更换背景图和背景颜色

    IntelliJ IDEA 更换背景图和背景颜色IntelliJIDEA 更换背景图和背景颜色

    2026年3月19日
    2
  • OpenClaw 爆火后,我做了个用自然语言管理 VMware 的 AI 技能

    OpenClaw 爆火后,我做了个用自然语言管理 VMware 的 AI 技能

    2026年3月13日
    3
  • 微机原理考试试题及答案分析_微机原理及接口技术

    微机原理考试试题及答案分析_微机原理及接口技术微机原理与接口技术期末考试试题及答案微机原理与接口技术期末考试题库微机系统的硬件由哪几部分组成?答:三部分:微型计算机(微处理器,存储器,I/0接口,系统总线),外围设备,电源。什么是微机的总线,分为哪三组?答:是传递信息的一组公用导线。分三组:地址总线,数据总线,控制总线。8086/8088CPU的内部结构分为哪两大模块,各自的主要功能是什么?答:总线接口部件(BIU)功能:根据执行单元EU的…

    2026年4月16日
    6
  • samba服务器创建用户_局域网nas搭建

    samba服务器创建用户_局域网nas搭建服务端白色背景192。168。100.第一步配置SAMBA创建新用户穿件共享文件夹和配置创建共享文件,给文件夹分组重启服务systemctlstartsmb第二步进入客户端配置查看挂载是否成功第三步进行验证先在主机端进入文件夹创建文件,再在客户端查看第四步在客户端进行验证通过验证证明了客户端和服务端能相互创建…

    2026年3月9日
    4

发表回复

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

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