原题链接

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

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

参考代码:

  1. #include<iostream>
  2. #include<string>
  3. #include<stack>
  4.  
  5. using namespace std;
  6.  
  7. stack<string> back;
  8. stack<string> forw;
  9.  
  10. int main(){
  11. int n;
  12. cin>>n;
  13. while(n--){
  14. string cmd,url="http://www.acm.org/";
  15. while(!back.empty())
  16. back.pop();
  17. while(!forw.empty())
  18. forw.pop();
  19. back.push(url);
  20.  
  21. while(1){
  22. cin>>cmd;
  23. if(cmd=="QUIT")break;
  24. if(cmd=="VISIT"){
  25. cin>>url;
  26. back.push(url);
  27. cout<<url<<endl;
  28. while(!forw.empty()) //visit new website, empty the forward stack
  29. forw.pop();
  30. }
  31. if(cmd=="BACK"){
  32. if(back.size()==1) //top url is the current
  33. cout<<"Ignored"<<endl;
  34. else{
  35. forw.push(url);
  36. back.pop();
  37. url=back.top();
  38. cout<<url<<endl;
  39. }
  40. }
  41. if(cmd=="FORWARD"){
  42. if(forw.empty())
  43. cout<<"Ignored"<<endl;
  44. else{
  45. url=forw.top();
  46. forw.pop();
  47. back.push(url);
  48. cout<<url<<endl;
  49. }
  50. }
  51. }
  52. if(n>0)
  53. cout<<endl;
  54.  
  55. }
  56.  
  57. return 0;
  58. }

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. web api control注册及重写DefaultHttpControllerSelector、ApiControllerActionSelector、ApiControllerActionInvoker

    namespace EWorkpal.WebApi { public class HttpNotFoundDefaultHttpControllerSelector : DefaultHttpCont ...

  2. visualSVN server库迁移

    首先,VisualSVN Server Manager,包含两个路径,一个是安装路径,例如本机:C:\Program Files\VisualSVN Server,一个是库路径,例如本机:C:\Rep ...

  3. ubuntu下的第一个脚本file.sh

    1.新建空文档,写入shell命令: #!/bin/sh cd /home/plg ./usb 第一行一定要有,一开始参考网上写的#!/usr/bin/sh,会提示错误 bash: ./file.sh ...

  4. 哪些字符需要urlencode编码?具体怎么处理?

    哪些字符需要urlencode编码?具体怎么处理? JS用escape()/encodeURI()/encodeURIComponent()方法编码,用unescape()/decodeURI()/e ...

  5. Android ContentProvider的实现

    当Android中的应用需要访问其他应用的数据时,用ContentProvider可以很好的解决这个问题.今天介绍一下ContentProvider的用法. 首先开发ContentProvider有两 ...

  6. acvitity的日常 启动模式(上)

    1. 基本介绍 大家平时只要懂一点Android知识的话,都一定会知道,一个应用的组成,往往包含了许多的activity组件,每个activity都应该围绕用户的特定动作进行跳转设计.比如说,一个电话 ...

  7. char 数组和 int 之间转化

    上周工作结束,来到斯凯网络也将近半个月来. 没有新人的感念,最多的是将自己当作一个战士. 废话不多说,直接入正题,在没有仔细考虑问题之前我们总会 觉得:这尼玛的有毛线难度啊,不就是一个 int 转为c ...

  8. jQuery 常用动画

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 理解Mac和iOS中的 Event 处理

    根据现在的理解,我把event处理分为5部分,第一是,Event处理的Architecture:第二是,Event的Dispatch到first responder之前: 第三是,Event从firs ...

  10. Ant、Maven、Gradle

    android Gradle project http://www.ibm.com/developerworks/cn/opensource/os-cn-gradle/ http://www.voge ...