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


相关推荐

  • Linux相关操作

    Linux相关操作

    2021年7月7日
    83
  • Mysql 截取字符串总结

    Mysql 截取字符串总结MySQL字符串截取函数:left(),right(),substring(),substring_index()。还有mid(),substr()。其中,mid(),substr()等价于substring()函数,substring()的功能非常强大和灵活。1.字符串截取:left(str,length)2.字符串截取:right(str,length)  …

    2022年6月9日
    27
  • 学习JavaScript这一篇就够了[通俗易懂]

    学习JavaScript这一篇就够了[通俗易懂]目录第一章JavaScript简介1.1、JavaScript的起源1.2、JavaScript的组成1.3、JavaScript的特点1.4、JavaScript的使用1.4.1、标签引用1.4.2、文件引用1.5、JavaScript的输出1.5.1、页面输出1.5.2、控制台输出1.5.3、弹出窗口输出1.6、JavaScript的注释1.6.1、单行注释1.6.2、多行注释第二章JavaScript基础语法2.1、标识符2.2、字面量和变量2.2.1、字面量2.2.2、变量2.3、数据类型2.3

    2022年6月16日
    28
  • emgucv教程(iis配置步骤)

    首先感谢qq群512782650,这是一个Emgucv爱好者创立的群,里面确实有许多爱好者。这篇博客旨在教学Emgucv3.0的安装与配置。环境:vs2015+Emgucv3.0EmguCv简介: EmguCV是.NET平台下对OpenCV图像处理库的封装。也就是OpenCV的.NET版。它运行在.NET兼容的编程语言下调用OpenCV的函数,

    2022年4月13日
    74
  • mac安装Android SDK

    mac安装Android SDK1、利用Androidsdk包进行安装:下载Androidsdk包后,点击tools下的android执行文件,SDKmanager打开空白。看网上教程需要重装sdk解决。重装sdk后,SDKmanager打开正常但无法选中各安装包前的复选框和安装按钮(整个SDKmanager点击无响应)。这个问题长时间未解决,所以放弃了此种方法。2、利用Androidcommand-line-tools安装。这种方法大概思路是:下载Androidcmdline-tools->用下载包中的s

    2022年7月21日
    14
  • OSI模型「建议收藏」

    OSI模型「建议收藏」定义开放式系统互联通信参考模型(OpenSystemInterconnectionReferenceModel,缩写为OSI),简称为OSI模型。该模型是由ISO(国际标准化组织)定义,是个灵活稳健和可互操作的模型。目的规范不同系统的互联标准,使两个不同的系统能够较容易通信,而不需要改变底层的硬件和软件的逻辑。优点每层功能简单单一,标准化允许各种类型的网络硬件和软件相互通信…

    2022年10月23日
    0

发表回复

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

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