Web Navigation poj 1028

题目链接http://poj.org/problem?id=1028

Web Navigation

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 25407

Accepted: 11317

Description

Standard webbrowsers contain features to move backward and forward among the pages recentlyvisited. One way to implement these features is to use two stacks to keep trackof the pages that can be reached by moving backward and forward. In thisproblem, 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 fromthe top of the backward stack, making it the new current page. If the backwardstack is empty, the command is ignored. 
FORWARD: Push the current page on the top of the backward stack. Pop the pagefrom the top of the forward stack, making it the new current page. If theforward stack is empty, the command is ignored. 
VISIT : Push the current page on the top of the backward stack, and makethe 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 URLhttp://www.acm.org/

Input

Input is asequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are allin uppercase. URLs have no whitespace and have at most 70 characters. You mayassume that no problem instance requires more than 100 elements in each stackat any time. The end of input is indicated by the QUIT command.

Output

For each commandother than QUIT, print the URL of the current page after the command isexecuted if the command is not ignored. Otherwise, print "Ignored".The output for each command should be printed on its own line. No output isproduced 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

 


题意:就像浏览器的前进,后退,浏览新页面的功能,用2个栈实现,一个存放前进的页面,一个存放后退的页面


#include <iostream>
#include<stdio.h>
#include<stack>
#include<string>
#include<string.h>
using namespace std;

struct strWeb
{
    char str[110];
};


stack<string> sIn;
stack<string> sOut;

int main()
{
    string strInit = "http://www.acm.org/";
    string ERROR = "Ignored";
    sIn.push(strInit);
    while(true)
    {
        string operate;
        cin>>operate;
        if(operate[0]=='Q')
            return 0;
        else if(operate[0]=='V')
        {//访问网址
            string url;
            cin>>url;
            sIn.push(url);
            /*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. */
            while(!sOut.empty())
                sOut.pop();
            cout<<sIn.top()<<endl;
        }
        else if(operate[0]=='F')
        {//前进
            if(sOut.size()>0)
            {
                sIn.push(sOut.top());
                sOut.pop();
                cout<<sIn.top()<<endl;
            }
            else
            {
                cout<<ERROR<<endl;
            }
        }
        else if(operate[0]=='B')
        {//回退
            if(sIn.size()>1)
            {//第一个值不回退
                sOut.push(sIn.top());
                sIn.pop();
                cout<<sIn.top()<<endl;
            }
            else
            {
                cout<<ERROR<<endl;
            }
        }

    }

    return 0;
}








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值