1113 - Discover the Web (栈)

      1113 - Discover the Web
Time Limit: 1 second(s)Memory Limit: 32 MB


Standard web browsers contain features to move backward andforward among the pages recently visited. One way to implement these featuresis to use two stacks to keep track of the pages that can be reached by moving backward and forward. You are asked toimplement this. The commands are:

1.      BACK:If the backward stack is empty, the command is ignored. Otherwise, push thecurrent page on the top of the forward stack. Pop the page from the top of thebackward stack, making it the new current page.

2.      FORWARD:If the forward stack is empty, the command is ignored. Otherwise, push thecurrent page on the top of the backward stack. Pop the page from the top of theforward stack, making it the new current page.

3.      VISIT<url>: Push the current page on the top of the backward stack, and makethe URL specified the new current page. The forward stack is emptied.

4.      QUIT:Quit the browser.

The browser initially loadsthe web page at the URL 'http://www.lightoj.com/'

Input

Input starts with an integer T (≤ 100),denoting the number of test cases.

Each case contains some commands. The keywords BACK,FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and haveat most 50 characters. The end of case is indicated by the QUIT command and it shouldn'tbe processed. Each case contains at most 100 lines.

Output

For each case, print the case number first. For eachcommand, print the URL of the current page (in a line) after the command isexecuted if the command is not ignored. Otherwise, print'Ignored'

Sample Input

Output for Sample Input

1

VISIT http://uva.onlinejudge.org/

VISIT http://topcoder.com/

BACK

BACK

BACK

FORWARD

VISIT http://acm.sgu.ru/

BACK

BACK

FORWARD

FORWARD

FORWARD

QUIT

Case 1:

http://uva.onlinejudge.org/

http://topcoder.com/

http://uva.onlinejudge.org/

http://www.lightoj.com/

Ignored

http://uva.onlinejudge.org/

http://acm.sgu.ru/

http://uva.onlinejudge.org/

http://www.lightoj.com/

http://uva.onlinejudge.org/

http://acm.sgu.ru/

Ignored



栈,后进先出,,新技能GET。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <stack>
using namespace std;
char str[1000][110];
char s[100];


int main()
{


    int t, ncase=1;
    scanf("%d", &t);
    strcpy(str[0],"http://www.lightoj.com/");
    while(t--)
    {
        printf("Case %d:\n",ncase++);
        stack<int>in;
        stack<int>out;
        while(!in.empty())
        {
            in.pop();
        }
        while(!out.empty())
        {
            out.pop();
        }
        int cnt=0,now=0;
        while(scanf("%s",s))
        {
            if(strcmp(s,"QUIT")==0)
            {
                break;
            }
            if(strcmp(s,"VISIT")==0)
            {
                cnt++;
                scanf("%s",str[cnt]);
                in.push(now);
                now=cnt;
                while(!out.empty())
                {
                    out.pop();
                }
                printf("%s\n",str[cnt]);
            }
            else if(strcmp(s,"BACK")==0)
            {


                if(in.size()>0)
                {
                    out.push(now);
                    int k=in.top();
                    now=k;
                    in.pop();
                    printf("%s\n",str[k]);
                }
                else
                {
                    printf("Ignored\n");
                }
            }
            else
            {


                if(out.size()>0)
                {
                    in.push(now);
                    int k=out.top();
                    now=k;
                    out.pop();
                    printf("%s\n",str[k]);
                }
                else
                {
                    printf("Ignored\n");
                }
            }
        }
    }
    return 0;
}

.

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值