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


相关推荐

  • [渝粤教育] 徐州工业职业技术学院 橡胶原材料 参考 资料「建议收藏」

    [渝粤教育] 徐州工业职业技术学院 橡胶原材料 参考 资料「建议收藏」教育-橡胶原材料-章节资料考试资料-徐州工业职业技术学院【】课程认知随堂测验1、【多选题】下列制品可采用橡胶材料制作的是。A、轮胎B、鞋子底C、输送带D、婴儿奶嘴参考资料【】2、【多选题】硫化体系主要包括。A、硫化剂B、促进剂C、活性剂D、防焦剂参考资料【】3、【判断题】橡胶是一种材料,它在大的形变下能迅速而有力恢复其形变,能够被改性(硫化)。A、正确B、错误参考资料【】4、【判断题】生胶是一种高弹性高聚物材料,是制造橡胶制品的基础材料,一

    2022年10月2日
    3
  • Spatial Transformer Networks(STN)理解

    Spatial Transformer Networks(STN)理解文章目录STN的作用STN的基本结构前向过程Tensorflow部分实现代码实验结果DistortedMNISTGermanTrafficSignRecognitionBenchmark(GTSRB)datasetSTN的作用之前参加过一个点云数据分类的比赛,主要借鉴了PointNet的网络结构,在PointNet中使用到了两次STN。点云数据存在两个主要问题:1、无序性:点云本…

    2022年10月19日
    3
  • Qt高并发

    Qt高并发QThread是一个低级(low-level)类,适合用于显式地构建长期运行的线程。     QtConcurrent是一个命名空间,提供了用于编写并发软件的更高层次的类和算法。该命名空间中有一个重要的类,QThreadPool,这是一个管理线程池的类。每个Qt应用程序都有一个QThreadPool::globalInstance()函数,它带有一个推荐的最大线程数,在大多数系统上,处理核的数…

    2022年5月28日
    116
  • 三维重建技术概述_CT三维重建不包括

    三维重建技术概述_CT三维重建不包括基于视觉的三维重建,指的是通过摄像机获取场景物体的数据图像,并对此图像进行分析处理,再结合计算机视觉知识推导出现实环境中物体的三维信息。1.相关概念(1)彩色图像与深度图像彩色图像也叫作RGB图像,R、G、B三个分量对应于红、绿、蓝三个通道的颜色,它们的叠加组成了图像像素的不同灰度级。RGB颜色空间是构成多彩现实世界的基础。深度图像又被称为距离图像,与灰度图像中像素点存储亮度值不同,其像素点存储的

    2025年6月18日
    2
  • Jafka来源分析——Processor

    Jafka来源分析——Processor

    2022年1月12日
    52
  • PFX 证书转 JKS

    PFX 证书转 JKS在阿里云上申请了一个SSL证书(Symantec免费版SSL),下载下来的是pfx格式的证书。使用的是jetty服务器,需要转换为jks格式。下载的压缩包:PFX证书转JKSkeytool-importkeystore-srckeystoremycert.pfx-srcstoretypepkcs12-destkeystoremycert.jks-d…

    2022年5月20日
    46

发表回复

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

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