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


相关推荐

  • VS2010连接SQLite数据库[通俗易懂]

    VS2010连接SQLite数据库

    2022年2月21日
    55
  • lua字符串截取_lua删除数组元素

    lua字符串截取_lua删除数组元素localstr=”helloworld!”string.sub(str,1,string.len(str)-1)–hellowordstring.sub(str,起始位置,整个字符串的长度)–获取指定位置长度的字符串string.len(目标字符串)–获取字符串的长度

    2022年10月6日
    0
  • 跳转网站_redirect

    跳转网站_redirectCreatedbyJerryWangonJun07,2014在browser里输入httpurl之后,敲回车之后http自动转换成https:通过httpwatch观察到有一个307redirect:通过tcodeSMICM查看ICMserver设置:发现server设置为所有http的request都会自动redirect成https,portnum…

    2025年6月13日
    0
  • cnpm安装与vue安装[通俗易懂]

    cnpm安装与vue安装[通俗易懂]首先是cnpm安装,在安装之前首先确定安装好了nodejs并且配置好了环境终端cmd后输入npm查看版本确定结果正确后进行cnpm安装终端输入npminstall-gcnpm–registry=https://registry.npm.taobao.org这里安装的是淘宝的镜像,等待安装完成后输入cnpm-v查看版本如果出现不是命令检查你的path路径确定安装成功后,终端键入:cnpminstallvue等待安装完成键入vue-V查看安装是否成功默认安装

    2022年10月15日
    0
  • 交换机telnet配置

    交换机telnet配置

    2021年7月30日
    67
  • 将整型变量转化为字符串_字符转字符串

    将整型变量转化为字符串_字符转字符串strsep(&data,”,”); //字符串切割函数kstrtoint(first,10,&duty_cycle);//字符串转整形10:十进制sprintf(data,”%d,%d”,duty_cycle,fan_freq);//整型数转字符串示例代码:ssize_tdcfan_write(structfile*file,constchar__user…

    2022年10月18日
    0

发表回复

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

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