1028 Web Navigation

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

题意: 模拟浏览器的前进/后退/访问/退出 的四个操作. 输出当前访问的URL或者Ignore(如果不能前进/后退).

分析:  用一个vector加上当前位置索引index即可. 当进行visit一个新的URL时, 应该基于当前URL重新建立FORWORD表(清空以前的FORWORD元素即可).

教训: 操作容器时, 如果对容器进行改变, 那么对应的size()等等也要考虑变化, 否则机会出错.

#include <iostream>
#include <vector>
using namespace std;
vector<string> vs;
int main(){
    string cmd;
    string addr;
    int index = 0;
    vs.push_back(string("http://www.acm.org/"));
    while(cin>>cmd && cmd != "QUIT"){
        if(cmd == "VISIT"){
            cin>>addr;
            /* 这样会wa,因为pop_back()操作会影响到for循环中的条件vs.size()的改变.
            if(index != vs.size()-1){
                for(int i=0;i<vs.size()-index-1;++i){
                    vs.pop_back();
                }
            }
            */
            while(index < vs.size()-1){
                vs.pop_back();
            }
            vs.push_back(addr);
            index++;
            cout<<vs[index]<<endl;
        }else if(cmd == "BACK"){
            if(index==0){
                cout<<"Ignored"<<endl;
            }else{
                index--;
                cout<<vs[index]<<endl;
            }
        }else if(cmd == "FORWARD"){
            if(index==vs.size()-1){
                cout<<"Ignored"<<endl;
            }else{
                index++;
                cout<<vs[index]<<endl;
            }
        }else{
            cout<<"Ignored"<<endl;
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/roger9567/p/4887093.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值