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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 解决网页文字无法选中或复制的方法_复制不了的文字

    解决网页文字无法选中或复制的方法_复制不了的文字我们在查看一些网页时会遇到不能复制的问题,或者鼠标无法选中文字,导致不能复制。这时候我们按下键盘的F12,点击console控制台,输入以下代码后回车即可vareles=document.getElementsByTagName(‘*’);for(vari=0;i<eles.length;i++){eles[i].style.userSele…

    2022年10月13日
    3
  • jar中没有主清单属性 (亲测有效)[通俗易懂]

    jar中没有主清单属性 (亲测有效)[通俗易懂]<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.8</source><target>1.8</target></.

    2025年9月14日
    8
  • tcpip3次握手的通信过程是_tcp协议为什么要三次握手

    tcpip3次握手的通信过程是_tcp协议为什么要三次握手关于tcp通信过程中的三次握手、四次挥手的过程三次握手:此过程中:第一次握手,客户端先发一个SYN请求并附带一个J的值给服务端第二次握手,服务端收到请求后解堵塞,发送一个SYN请求并附带一个K值,还发送了第一次握手后对客户端的响应包并附带在之前接收到的J值的基础上加上1,即J+1第三次握手,客户端收到服务端发来的SYN请求和K值后,再发送一个K+1的响应包给服务端至此,三次握手成功…

    2022年8月18日
    5
  • 使用python实现京东抢购脚本

    使用python实现京东抢购脚本环境:python3.7浏览器:火狐在pythonscripts文件夹下边放入相应版本的geckodrivercmd页面:pipinstallseleniumpipinstalldatetime事先需要把商品放入购物车,电脑时间和北京时间一致fromseleniumimportwebdriverimporttimeimportda…

    2022年6月18日
    73
  • elasticsearch(es)的安装-macOs

    elasticsearch(es)的安装-macOs前提:已经安装过jdk1.8java-version#查看jdk版本1.es的安装和访问es安装brewinstallelasticsearch#安装brewinfoelasticsearch#查看es信息brewservicesstartelasticsearch#启动浏览器输入:localhost:9200查看es2.kibana的安装和访问kibana可以通过可视化的界面操作访问eskibana安装brewinstallkiban

    2022年6月22日
    37
  • Vue3快速入门教程「建议收藏」

    Vue3快速入门教程「建议收藏」DataProperty模板在<>内的,属于HTMLattribute普通的Mustache语法:双大括号->{{number}}的文本v-bind和v-on的使用创建vm实例时对常用的几个钩子函数的使用方法created(){}mounted(){}以下示例:每秒改变1次msg<!DOCTYPEhtml><htmllang=”en”><head><metacharset=”U..

    2022年5月4日
    37

发表回复

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

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