【POJ - 1028】 Web Navigation( 栈 or 模拟队列 )

题干:

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;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441977.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python循环post请求_循环post请求太多

我正在做一个scrapy spider&#xff0c;我必须发送一个post请求循环才能转到下一个页面&#xff0c;问题是它只发送一个post请求。querystring更改每个页面的元素“currentPage”&#xff0c;因此我必须为每个页面更改此键的值并发送post。但是&#xff0c;正如我之前所说&…

【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)

题干&#xff1a; Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. Farmer Jo…

递归Java_递归的Java实现

递归是一种应用非常广泛的算法(或者编程技巧)。递归求解问题的分解过程&#xff0c;去的过程叫“递”&#xff0c;回来的过程叫“归”。递归需要满足的三个条件&#xff1a;1. 一个问题的解可以分解为几个子问题的解&#xff1b;2. 这个问题与分解之后的子问题&#xff0c;除了…

【HDU - 2112】 HDU Today(dijkstra单源最短路 + map转换)

题干&#xff1a; HDU Today Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 11 Accepted Submission(s) : 5 Problem Description 经过锦囊相助&#xff0c;海东集团终于度过了危机&#xff0c;从此&#…

JAVA线程并发数量控制_线程同步工具(二)控制并发访问多个资源

声明&#xff1a;本文是《 Java 7 Concurrency Cookbook》的第三章&#xff0c; 作者&#xff1a; Javier Fernndez Gonzlez 译者&#xff1a;郑玉婷控制并发访问多个资源在并发访问资源的控制中&#xff0c;你学习了信号量(semaphores)的基本知识。在上个指南&#xff0c;你实…

*【51nod - 1459】迷宫游戏(记录双向权值的Dijkstra单源最短路)

题干&#xff1a; 你来到一个迷宫前。该迷宫由若干个房间组成&#xff0c;每个房间都有一个得分&#xff0c;第一次进入这个房间&#xff0c;你就可以得到这个分数。还有若干双向道路连结这些房间&#xff0c;你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定…

算法--背包九讲(详细讲解+代码)

背包九讲 目录 第一讲 01背包问题 第二讲 完全背包问题 第三讲 多重背包问题 第四讲 混合三种背包问题 第五讲 二维费用的背包问题 第六讲 分组的背包问题 第七讲 有依赖的背包问题 第八讲 泛化物品 第九讲 背包问题问法的变化 附&#xff1a;USACO中的背包问题 前…

java中白盒测试用例_基于JAVA开发的中国象棋游戏的开发与研究白盒测试用例.doc...

中国象棋白盒测试用例文件状态当前版本V1.0草稿作 者梁世聪完成日期2012/6/17文档模板SSP-VER-T13-V1.0密 级变更历史版本完成日期变更记录作者批准签字V1.02012/6/17无梁世聪梁世聪目 录目录1 目的12 范围13 被测模块列表14 模块逻辑结构14.1 模块逻辑结构图14.2 模块功能定义…

【POJ - 1502】MPI Maelstrom(Dijkstra单源最短路--求一点到其余个点的最小值的最大值)

题干&#xff1a; BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKees research advisor, Jack Swigert, has asked her to ben…

java 强制清除缓存_IDEA强制清除Maven缓存的方法示例

重新导入依赖的常见方式下面图中的刷新按钮&#xff0c;在我的机器上&#xff0c;并不能每次都正确导入pom.xml中写的依赖项&#xff0c;而是导入之前pom.xml的依赖(读了缓存中的pom.xml)。当然除了这些&#xff0c;还可以下面这样&#xff1a;存在的问题上面虽然是重新导入Mav…

ACM算法--spfa算法--最短路算法

求单源最短路的SPFA算法的全称是&#xff1a;Shortest Path Faster Algorithm。 SPFA算法是西南交通大学段凡丁于1994年发表的。 从名字我们就可以看出&#xff0c;这种算法在效率上一定有过人之处。 很多时候&#xff0c;给定的图存在负权边&#xff0c;这时类似…

knn算法python理解与预测_理解KNN算法

KNN主要包括训练过程和分类过程。在训练过程上&#xff0c;需要将训练集存储起来。在分类过程中&#xff0c;将测试集和训练集中的每一张图片去比较&#xff0c;选取差别最小的那张图片。如果数据集多&#xff0c;就把训练集分成两部分&#xff0c;一小部分作为验证集(假的测试…

【POJ-3259】 Wormholes(判负环,spfa算法)

题干&#xff1a; While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac…

【HihoCoder - 1550】顺序三元组(思维)

题干&#xff1a; 给定一个长度为N的数组A[A1, A2, ... AN]&#xff0c;已知其中每个元素Ai的值都只可能是1, 2或者3。 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ N且Ai < Aj < Ak。 Input 第一行包含一个整数N 第二行包含N个整数A1, A2, ... …

joptionpane java_Java JOptionPane

Java JOptionPane1 Java JOptionPane的介绍JOptionPane类用于提供标准对话框&#xff0c;例如消息对话框&#xff0c;确认对话框和输入对话框。这些对话框用于显示信息或从用户那里获取输入。JOptionPane类继承了JComponent类。2 Java JOptionPane的声明public class JOptionPa…

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

题干&#xff1a; One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roa…

【HDU - 3342】Legal or Not(拓扑排序)

题干&#xff1a; ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas.…

java 股票 代码_Java中利用散列表实现股票行情的查询_java

---- 在java中&#xff0c;提供了一个散列表类Hashtable&#xff0c;利用该类&#xff0c;我们可以按照特定的方式来存储数据&#xff0c;从而达到快速检索的目的。本文以查询股票的收盘数据为例&#xff0c;详细地说明java中散列表的使用方法。一、散列表的原理---- 散列表&am…

deepin部署python开发环境_deepin系统下部署Python3.5的开发及运行环境

deepin系统下部署Python3.5的开发及运行环境1 概述本人小白一枚&#xff0c;由于最近要学习python接口自动化测试&#xff0c;所以记录一下相关学习经过及经验&#xff0c;希望对跟我一样小白的朋友可以有所帮助。2 下载在python官网下载指定平台下的python3.5的环境wget https…

【HDU - 2066】:一个人的旅行(Dijkstra算法)

题干&#xff1a; 虽然草儿是个路痴&#xff08;就是在杭电待了一年多&#xff0c;居然还会在校园里迷路的人&#xff0c;汗~),但是草儿仍然很喜欢旅行&#xff0c;因为在旅途中 会遇见很多人&#xff08;白马王子&#xff0c;^0^&#xff09;&#xff0c;很多事&#xff0c;还…