POJ1028 Web Navigation

Web Navigation

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 35451 Accepted: 15869

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

【思路】需要两个栈,一个用于存放后退的页面,一个用于存放前进的页面。同时本题有多个注意点:

(1)当你输入一个URL地址时,就一定有后退的页面,同时肯定没有前进的页面。

(2)当你点击后退时候肯定会产生前进的页面,同理前进时候肯定会产生后退的页面。

(3)嗯...其他的大概备注在程序里了。

【代码】AC的C++代码如下:

#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
/*  注意事项:
访问一个URL后就没有前进的页面,但是有后退的页面。
后退后就产生前进的页面。
前进完会产生后退的页面。
*/
int main()
{
    stack<string> stack1,stack2;
    string s,url;
    string init_page = "http://www.acm.org/";
    stack1.push(init_page);
    while (cin >> s)
    {
        if (s == "QUIT")
            break;
        else if (s == "BACK")
        {
            //若有两个以上元素,则最顶端为当前访问的页面,需要先弹出,然后下一个为返回后的页面
            if (stack1.size() >= 2) //只有两个以上的URL存在时,返回键才有效
            {
                stack2.push(stack1.top());
                stack1.pop();   //先弹出当前路径,输出下一个路径
                cout << stack1.top() << endl;
            }
            //若只有一个元素,则当前位置已经为初始页面没法返回。
            else
            {
                cout << "Ignored" << endl;
            }
        }
        //访问一个URL后就没有前进的页面,需要将stack2清空
        else if (s == "VISIT")
        {
            cin >> url;
            stack1.push(url);
            cout << url << endl;
            while (!stack2.empty())
                stack2.pop();
        }
        /*
            stack1 : c b a (top)
            back : a -> b -> c      stack2 : a b c (top)
            forward : c -> b -> a

        */
        else if (s == "FORWARD")
        {
            if (!stack2.empty())
            {
                //前进完会产生后退的页面
                stack1.push(stack2.top());
                cout << stack2.top() << endl;
                stack2.pop();
            }
            else
            {
                cout << "Ignored" << endl;
            }
        }
        else
        {

        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值