zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    失落时就励志一下
    域名续费一年
    BIM新时代背景下的建筑业技术变革
    培养自信的自己
    吧台人很多
    BlogEngine学习一:操作符重载
    CSS Sprite打造个性化导航
    WinForm中的DataGridView子窗体刷新父窗体备忘
    WinForms下的SliderButtons设计
    GridView中实现DropDownList联动
  • 原文地址:https://www.cnblogs.com/roger9567/p/4887093.html
Copyright © 2011-2022 走看看