周末小测——C(ZOJ 1061)

1 篇文章 0 订阅

这道题,完全是现学现卖。

看到题目,马上想到了栈的结构,由于自己笨拙,果断放弃了自己写个栈的想法。

直接转向了STL  <vector>无奈用的不熟练,只能看着打印的资料,现学现卖。

C——ZOJ 1061 (Web Navigation)

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
题意分析:
模拟了网页访问的过程。
有四种操作:
VISIT:表示浏览网页,需要被压入栈中。
BACK:返回,即返回当前网页的上一网页。
FORWARD:前一网页,你访问当前网页的前一网页。
QUIT:结束。
注意:
初始网站为:“http://www.acm.org/
当BACK,或者FORWORD的时候超出栈中的元素范围,输出Ignored。
当返回后,又浏览新的网页的时候,之前的网页需要被删除掉。
例:测试实例给出的顺序为:
http://www.acm.org/
http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
一开始按照这个顺序存储。
执行下列操作:
FORWARD
VISIT http://www.ibm.com/
在返回http://acm.ashland.edu/该网页的时候,又浏览了 http://www.ibm.com/
这时候栈里存的应该是
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
这个顺序。只要注意到这几点,就很easy了。
代码:
#include <iostream>
#include <vector>
#include <string.h>
#include <string>
#include <stdio.h>
using namespace std;
vector<string> s;
int main()//貌似用switch更加漂亮
{
    int i=0;
    string c,t;
    s.push_back("http://www.acm.org/");//初始网页要放到最底下
    while(cin>>c)//string的输入用C++的
    {
        if(c=="QUIT")//离开操作
            break;
        if(c=="VISIT")//访问
        {
            i++;//简单代替指针,标志现在走到了几个的位置
            cin>>t;//i起了关键性作用
           for(int j=s.size()-i-1;j>=0;j--)//因为当前访问的网页,总是应该在栈尾
            s.pop_back();//因此把在此之前的出栈
            s.push_back(t);//压入栈中
            cout<<t<<endl;
        }
        if(c=="BACK")
        {
            i--;//返回,当前位置-1
            if(i<0){//没有元素了
                cout<<"Ignored"<<endl;//输出提示信息
                i++;//并将位置返回
            }//要先检查,不然会引起错误
            else
            cout<<s[i]<<endl;
        }
        if(c=="FORWARD")
        {
            i++;
            if(i>=s.size()){//同上,只是判断标准不同而已
                cout<<"Ignored"<<endl;
                i--;
            }
            else
            cout<<s[i]<<endl;
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值