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;
}

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • css3实现光标悬浮滚动菜单

    效果:http://hovertree.com/texiao/css3/21/本文所用到的CSS知识请点击效果展示也中第一和第二个链接。代码:转自:http://hovertree.com/h/bj

    2021年12月24日
  • flyweight设计模式_适配器模式菜鸟

    flyweight设计模式_适配器模式菜鸟亨元模式动机模式定义实例结构要点总结笔记。动机在软件系统采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行时代价—主要指内存需求方面的代价如何在避免大量细粒度对象问题的同时,让外部客户仍然能够透明地使用面向对象地方式来进行操作模式定义运用共享技术有效地支持大量细粒度地对象。实例每一个字符都是一个字体 字体对象Fontclass Font{private: //unique object key string key; //object

  • PyQuery常用用法总结

    PyQuery常用用法总结什么是PyQueryPyQuery是一个类似于jQuery的解析网页工具,使用lxml操作xml和html文档,它的语法和jQuery很像。和XPATH,BeautifulSoup比起来,PyQuery更加灵活,提供增加节点的class信息,移除某个节点,提取文本信息等功能。初始化PyQuery对象html文档的所有操作都需要PyQuery对象来完成,初始化PyQuery对象主要有三种方式…

  • 通信加密算法

    通信加密算法1.加密算法分类加密算法通常分为对称性加密算法和非对称性加密算法。对于对称性加密算法,信息接收双方都需事先知道密匙和加解密算法且其密匙是相同的,之后便是对数据进行加解密了。非对称算法与之不同,发送双方A,B事先均生成一堆密匙,然后A将自己的公有密匙发送给B,B将自己的公有密匙发送给A,如果A要给B发送消息,则先需要用B的公有密匙进行消息加密,然后发送给B端,此时B端再用自己的私有密匙进行消息解密…

  • 远程连接oracle01017,sqlplus远程sys用户登录ora 01017的解决方法[通俗易懂]

    远程连接oracle01017,sqlplus远程sys用户登录ora 01017的解决方法[通俗易懂]UsingORAPWDWhenyouinvokethispasswordfilecreationutilitywithoutsupplyinganyparameters,youreceiveamessageindicatingtheproperuseofthecommandasshowninthefollowingsampleoutput…

  • vuerouter配置_vue面包屑

    vuerouter配置_vue面包屑介绍VueRouter是Vue.js官方的路由管理器。它和Vue.js的核心深度集成,让构建单页面应用变得易如反掌。包含的功能有:嵌套的路由/视图表模块化的、基于组件的路由配置路由参

发表回复

您的电子邮箱地址不会被公开。

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