原题链接

题目大意:模拟一个浏览器,打开一个网页、后退或者前进,输出网址。

解法:用两个堆栈分别表示后退保存的网页和前进保存的网页。初始化时把当前页面压入后退堆栈的栈顶。要注意几点,一个是每次记得要清空两个堆栈,另一个,如果后退之后又打开了新的网页,前进的堆栈要清空,这和真实的浏览器的结果是一致的。

参考代码:

#include<iostream>
#include<string>
#include<stack> using namespace std; stack<string> back;
stack<string> forw; int main(){
int n;
cin>>n;
while(n--){
string cmd,url="http://www.acm.org/";
while(!back.empty())
back.pop();
while(!forw.empty())
forw.pop();
back.push(url); while(1){
cin>>cmd;
if(cmd=="QUIT")break;
if(cmd=="VISIT"){
cin>>url;
back.push(url);
cout<<url<<endl;
while(!forw.empty()) //visit new website, empty the forward stack
forw.pop();
}
if(cmd=="BACK"){
if(back.size()==1) //top url is the current
cout<<"Ignored"<<endl;
else{
forw.push(url);
back.pop();
url=back.top();
cout<<url<<endl;
}
}
if(cmd=="FORWARD"){
if(forw.empty())
cout<<"Ignored"<<endl;
else{
url=forw.top();
forw.pop();
back.push(url);
cout<<url<<endl;
}
}
}
if(n>0)
cout<<endl; } return 0;
}

ZOJ 1061 Web Navigation的更多相关文章

  1. poj 1028 Web Navigation

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31088   Accepted: 13933 ...

  2. POJ1028 Web Navigation

    题目来源:http://poj.org/problem?id=1028 题目大意: 模拟实现一个浏览器的“前进”和“回退”功能.由一个forward stack和一个backward stack实现. ...

  3. POJ 1028:Web Navigation

    Web Navigation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30828   Accepted: 13821 ...

  4. POJ-1028 Web Navigation 和TOJ 1196. Web Navigation

    Standard web browsers contain features to move backward and forward among the pages recently visited ...

  5. Web Navigation

    Description Standard web browsers contain features to move backward and forward among the pages rece ...

  6. [POJ1028]Web Navigation(栈)

    这题是01年East Central North的A题,目测是签到题 Description Standard web browsers contain features to move backwa ...

  7. poj 1028 Web Navigation(模拟)

    题目链接:http://poj.org/problem? id=1028 Description Standard web browsers contain features to move back ...

  8. poj 1208 Web Navigation(堆栈操作)

    一.Description Standard web browsers contain features to move backward and forward among the pages re ...

  9. POJ 1028 Web Navigation 题解

    考查代码能力的题目.也能够说是算法水题,呵呵. 推荐新手练习代码能力. 要添加难度就使用纯C实现一下stack,那么就有点难度了,能够使用数组模拟环形栈.做多了,我就直接使用STL了. #includ ...

随机推荐

  1. HDU 1828 扫描线(矩形周长并)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  2. [开发笔记]-多线程异步操作如何访问HttpContext?

    如何获取文件绝对路径? 在定时器回调或者Cache的移除通知中,有时确实需要访问文件,然而对于开发人员来说, 他们并不知道网站会被部署在哪个目录下,因此不可能写出绝对路径, 他们只知道相对于网站根目录 ...

  3. struts中的请求数据自动封装

    Struts 2框架会将表单的参数以同名的方式设置给对应Action的属性中.该工作主要是由Parameters拦截器做的.而该拦截器中已经自动的实现了String到基本数据类型之间的转换工作.在st ...

  4. UITableViewCell Property “icon” cannot be found in forward class object “DJWeiBo”

    UITableViewCell 自定义表格 实体属性不显示错误 Property “icon” cannot be found in forward class object “DJWeiBo”引入实 ...

  5. GCD常用剖析

    介绍:Grand Central Dispatch 简称(GCD)是苹果公司开发的技术,以优化的应用程序支持多核心处理器和其他的对称多处理系统的系统.这建立在任务并行执行的线程池模式的基础上的.它首次 ...

  6. cometd的服务器配置

    CometDServlet必须在web.xml中进行配置,如下: <servlet>        <servlet-name>cometd</servlet-name& ...

  7. Java Inner class && nested class

    Java中,Inner Class(不被 static修饰)可以访问outer class 的所有成员(包括私有成员),同时,内部类 的创建必须经由外部类的实例! nested class 有stat ...

  8. redis补充和rabbitmq讲解

    线程池: redis发布订阅: rabbitMQ: MySQL; python pymysql: python orm SQLAchemy: paramiko: 堡垒机: 1.线程池 1.1 cont ...

  9. ios工程中ARC与非ARC的混合

    ARC与非ARC在一个项目中同时使用, 1,选择项目中的Targets,选中你所要操作的Target,2,选Build Phases,在其中Complie Sources中选择需要ARC的文件双击,并 ...

  10. 数据结构---设计一个栈,push, pop, min 时间复杂度都是 O(1)

    普通的栈,push, pop 操作的复杂度是 O(1), 但是如果要找出其中的最小值,则需要 O(N)的时间. 题目要求 min 复杂度也是 O(1), 做法便是 空间换时间,每一步栈的最小值都用一个 ...