题干:

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

来自一个博客的分析如下:http://www.mamicode.com/info-detail-428744.html

题意简述:这题是一个模拟WEB浏览器的程序,默认主页为http://www.acm.org/输入为指令,出书为当前页面的网址。
输入的指令有4种:(1)VISIT(后接一个网址)——访问它后面紧接着的网址。(2)BACK——访问当前网页的前一个。(3)FORWARD——访问当前网页的后一个。(4)QUIT——退出(关闭浏览器)。思路:必须记录访问过的网址,因为是顺序关系,所以用数组存储,下标为0的元素为默认页;有一个变量记录当前页面在数组中的位置;BACK和FORWARD指令可能出现特殊情况:(1)BACK到默认页面再BACK——输出Ignored。(2)FORWARD到最后一个访问到的页面再与FORWARD——输出Ignored。这样,就有一个问题:要记录最后一个访问到的页面,对于这个问题只需要在遇到VISIT指令的时候顺便操作上面的指令的对应操作就变得很简单。VISIT——就将网址写入数组后再进行输出,记录当前位置,这个位置也即最后一个访问到的页面。BACK——当前页面减1,输出网址,注意判断特殊情况。FORWARD——当前页面+1,输出网址,也要注意特殊情况。QUIT——退出。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>using namespace std;
//stack <string> sk;
char s[1000][100];
int main()
{
//  freopen("in.txt","r",stdin);char op[10];int top = 0,maxx = 0;strcpy(s[0],"http://www.acm.org/");while(scanf("%s",op) ) {if(!strcmp(op,"QUIT")) break;else if(op[0] == 'V') {scanf("%s",s[++top]);maxx = top;printf("%s\n",s[top]);}else if(op[0] == 'B') {if(top >= 1) {top--;printf("%s\n",s[top]);}else if(top == 0) {printf("Ignored\n");} }else if(op[0] == 'F') {top++;if(top>maxx) {printf("Ignored\n");top--;}else printf("%s\n",s[top]);}}return 0 ;
}

AC代码2:(栈)

#include <stdio.h>
#include <string.h>
const int maxn = 75, N = 10005;
char q[N][maxn], queue[N][maxn], str[maxn];
char ch[maxn];
int main ( )
{int T, top, front, cas = 0;//freopen ( "in0.in", "r", stdin );scanf ( "%d", &T );while ( T -- ){front = top = -1;strcpy ( q[++ top], "http://www.acm.org/" );//开始就将一个网址加入栈if ( cas ++ )   //每组数据有换行printf ( "\n" );while ( ~ scanf ( "%s", str ) && strcmp ( str, "QUIT" ) != 0 ){if ( strcmp ( str, "VISIT" ) == 0 ){   //查看时将所有可以后退的全部去掉scanf ( "%s", ch );printf ( "%s\n", ch );strcpy ( q[++ top], ch );front = -1;}else if ( strcmp ( str, "BACK" ) == 0 ){if ( top <= 0 ) //小于等于0证明不能在后退了printf ( "Ignored\n" );else{//将此网页加入可以前进的栈中strcpy ( queue[++ front], q[top] );printf ( "%s\n", q[-- top] );}}else{if ( front < 0 )printf ( "Ignored\n" );else{//将前进的网页加到后退中strcpy ( q[++ top], queue[front] );printf ( "%s\n", queue[front --] );}}}}return 0;
}

【POJ - 1028】 Web Navigation( 栈 or 模拟队列 )相关推荐

  1. POJ 1028: Web Navigation

    2019独角兽企业重金招聘Python工程师标准>>> 题目在此 解题思路:就是用栈来模拟浏览器的前进.后退操作. 代码: #include <cstdio>struct ...

  2. POJ 1028 Web Navigation

    题意:模拟一个网页浏览器的操作,BACK, FORWARD, VISIT, and QUIT. 思路:... 代码1:段.. //模拟法 #include<iostream> #inclu ...

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

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

  4. 百练OJ:1028:Web Navigation

    链接:http://bailian.openjudge.cn/practice/1028/ 1028:Web Navigation 描述 Standard web browsers contain f ...

  5. 数据结构——栈与队列操作(用栈模拟队列)

    [栈与队列操作] 问题描述:假设有两个长度相同的栈 S1,S2,已知以下入栈.出栈.判栈满和判栈空操作: void Push(S,x); Elemtype Pop(S); bool StackFull ...

  6. 使用栈Stack实现队列Queue

    如何只使用stack实现queue呢?由于stack是现进后出(FILO),而queue是先进先出的(FIFO).也就是说stack进行了一次反向,进行两次反向就能实现queue的功能,所以可以用两个 ...

  7. 【干货】容器适配器实现两个栈模拟队列

    用两个栈模拟队列的思想就是"倒水思想",这里我们用自定义类型模拟出线性表,再用线性表做容器实现栈的数据结构,最后用栈来实现队列,代码如下: #include<iostream ...

  8. 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)

    主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...

  9. I - Keylogger Gym - 101078I——模拟队列+栈

    Think: 1知识点:模拟队列+栈 2题意分析:字母输入移动,分别用队列记录输入光标之前内容,用栈记录光标之后可能存在的内容 3其余思路:链表模拟插入结点删除结点 vjudge题目链接 以下为Acc ...

最新文章

  1. ios 图片自动轮播
  2. 【MATLAB】三维图形绘制 ( 三维平面图 | 二维网格 | meshgrid 函数 | 绘制网格 | mesh 函授 | 绘制平面 | surf 函数 | 绘制等高线 | contour 函数 )
  3. 2017/11/3模拟赛
  4. firewall-cmd命令管理防火墙
  5. 高性能网站架构设计之缓存篇(6)- Redis 集群(中)
  6. LeetCode 1169. 查询无效交易
  7. BoW(词袋Bag of words)
  8. GDB中应该知道的几个调试方法-转
  9. java实现simhash算法
  10. FIFO算法与LRU算法软考试题
  11. [渝粤教育] 西南科技大学 经济法概论 在线考试复习资料2021版
  12. ARM开发6.3.4 基础实训( 4 ) 两个 LED 显示二位数(动态显示)--LPC21XX
  13. 傲腾内存 可以用ghost系统_英特尔傲腾是什么?让你秒懂英特尔傲腾技术
  14. 主成分分析二级指标权重_确定权重方法之一:主成分分析
  15. android 卡片行星,卡片详情
  16. AngularJS知识概括
  17. 离散信号的周期性判定,C++实现
  18. 成都拓嘉启远:多多进宝如何关
  19. 性感荷官在线发牌,真的靠谱吗?
  20. linux openssl 编译错误,“致命错误:openssl/opensslv.h:没有这样的文件或目录”编译mitmproxy...

热门文章

  1. 【数据结构与算法】计数、基数、桶 O(n) 不基于比较
  2. 5 html 根据手机转动而转动_手机安装陀螺仪有什么用 手机安装陀螺仪作用介绍【详解】...
  3. python入口函数的作用_python之函数中参数的作用域
  4. idea中连接mysql插入成功数据 在navicat中刷新表格没有数据_MySQL入门简记
  5. 回溯——伯努利装错信封问题
  6. yum安装ruby_centos 6.5 ruby环境安装
  7. BAT批处理中的字符串处理
  8. MFC工具栏增加EditBox和Button
  9. CosiWorksNew
  10. ubuntu修改用户名、计算机名、主目录名