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


相关推荐

  • sigaction函数解析

    sigaction函数解析sigaction函数的功能是检查或修改与指定信号相关联的处理动作(可同时两种操作)。他是POSIX的信号接口,而signal()是标准C的信号接口(如果程序必须在非POSIX系统上运行,那么就应该使用这个接口)给信号signum设置新的信号处理函数act,同时保留该信号原有的信号处理函数oldactint sigaction(int signo,

    2022年5月25日
    72
  • Python:画出笛卡尔心形曲线

    Python:画出笛卡尔心形曲线极坐标方程:ρ=a(1−sin⁡θ)\rho=a(1-\sin\theta)ρ=a(1−sinθ)极坐标画图:%matplotlibinlineimportmatplotlib.pyplotaspltimportnumpyasnptheta=np.linspace(0.0,2*np.pi,1000)a=5rho=a*(1-np.sin…

    2022年10月16日
    2
  • JS创建和存储 cookie的一些方法

    JS创建和存储 cookie的一些方法

    2021年10月31日
    44
  • MySQL索引原理及BTree(B-/+Tree)结构详解「建议收藏」

    MySQL索引原理及BTree(B-/+Tree)结构详解「建议收藏」目录摘要数据结构及算法基础索引的本质B-Tree和B+TreeB-TreeB+Tree带有顺序访问指针的B+Tree为什么使用B-Tree(B+Tree)主存存取原理磁盘存取原理局部性原理与磁盘预读B-/+Tree索引的性能分析MySQL索引实现MyISAM索引实现InnoDB索引实现索引使用策略及优化示例数据库最左前缀原理与…

    2022年6月24日
    27
  • ldapsearch命令详解_ldapsearch命令详解

    ldapsearch命令详解_ldapsearch命令详解参数用途-?打印关于使用ldapsearch的帮助。-aderef指定别名反向引用。请输入never、always、search或find。如果不使用此参数,缺省为never。-A只检索属性的名称,而不检索属性的值。-bbasedn指定用作搜索起始点的专有名称。使用引号来指定该值,例如:”ou=West,o=Acme,c=US”如果要搜索的服务器需要指定搜索…

    2025年6月16日
    2
  • 零基础如何学习PLC

    零基础如何学习PLC很多人在学习PLC之前都做过推销员、维修工、电工或出租车司机。很多新手转PLC的时候肯定会问这个问题:学习PLC的基础是什么?如何学习PLC编程入门?这个社会基础教育问题问的好,说明你对于学生学习这件事我们还是有常识的,基础发展起到一个铺垫、承前启后的作用,把这方面可以搞定了,你会比那些不在乎这些基础而直接进行强攻PLC的人要事半功倍的多。下面就来说说正事学习plc,首先必须有电工基础,了解一些低压电器及其控制技术,这是学习plc必备的基础知识。.原因:面对新机器,首先要了解硬件结

    2022年10月18日
    3

发表回复

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

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