Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342932
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-05-03 17:38:08

一、问题描述

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

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

VISIT

BACK

BACK

BACK

FORWARD

VISIT

BACK

BACK

FORWARD

FORWARD

FORWARD

QUIT

Sample Output

Ignored

Ignored

 

二、解题思路

使用一个字符串数组进行模拟。

三、代码

 

#include<iostream>
using namespace std;
char S[105][80];
int cur;
int top;
int main()
{
    cur=0;
    top=0;
    char cmd[10];
    strcpy(S[0],"");
    while(scanf("%s",&cmd))
    {
        if(strcmp(cmd,"QUIT")==0)
            break;
        if(strcmp(cmd,"VISIT")==0)
        {
            getchar();
            cur++;
            scanf("%s",&S[cur]);
            top=cur;
            printf("%s\n",S[cur]);
        }
        else
        {
            if(strcmp(cmd,"BACK")==0)
            {
                if(cur<1)
                    printf("Ignored\n");
                else
                {
                    cur--;
                    printf("%s\n",S[cur]);
                }

            }
            else
                if(strcmp(cmd,"FORWARD")==0)
                {
                    if(top==cur)
                        printf("Ignored\n");
                    else
                    {
                        cur++;
                        printf("%s\n",S[cur]);

                    }
                }
        }
    }
    return 0;
}


阅读(2249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~