poj 1028 Web Navigation

ACM比赛整理

共 3783字,需浏览 8分钟

 ·

2021-11-14 05:04

Web Navigation


Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 39651
Accepted: 17749

Description

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



网站导航


描述

标准的web浏览器包含在最近访问的页面中向前和向后移动的功能。实现这些特性的一种方法是使用两个堆栈来跟踪通过向前和向后移动可以到达的页面。在这个问题中,要求您实现这个。


需要支持以下命令:

BACK:将当前页面推到前向堆栈的顶部。从向后堆栈的顶部弹出页面,使其成为新的当前页面。如果反向堆栈为空,该命令将被忽略。

向前:将当前页面推到向后堆栈的顶部。从forward堆栈的顶部弹出该页,使其成为新的当前页。如果forward栈为空,则忽略该命令。

VISIT:将当前页面推到向后堆栈的顶部,并使URL指定新的当前页面。前向堆栈被清空。

QUIT:退出浏览器。

假设浏览器最初加载URL为http://www.acm.org/的网页


输入

输入是一系列命令。命令关键字BACK、FORWARD、VISIT和QUIT均为大写。url没有空格,最多70个字符。您可以假设任何时候每个堆栈中都不需要超过100个元素。输入的结束由QUIT命令指示。


输出

对于除QUIT外的其他命令,如果没有忽略该命令,则在命令执行后打印当前页面的URL。否则,打印“忽略”。每个命令的输出应该打印在它自己的行上。QUIT命令没有输出。


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



题意(水):做一个游览器的历史记录。

back:后退到上一个页面,当上一个页面没有时,输出ignored.

forward:向前一个页面,但此页面为最前的页面时,输出ignored. 

vista:游览所指定的页面。

quit:退出。

解题思路:题目是说用栈,但不用栈也是可以的。就用纯数组来模拟就栈。

我用的是string类型,这个类型有个好处,就是比较不用strcmp函数,还有输入方便,直接cin就行。但其实本质就是char[];


代码:

#include 
#include
#include
#include

using namespace std;

string str[1000],str1[1000]; //记得数组不能开太小,开太小就会RE。

int main()
{
int n=0,mark=0,now=0;
str1[0]="http://www.acm.org/"; //题目说最原始的页面就是这个acm的。
while(cin>>str[n]&&str[n]!="QUIT")
{
if(str[n]=="VISIT")
{
mark++;
now++;
if(mark>now)
mark=now; //页面的覆盖。
cin>>str1[mark];
}
if(str[n]=="VISIT")
{
cout< }
if(str[n]=="BACK")
{
if(now==0)
cout<<"Ignored"< else
cout< }
if(str[n]=="FORWARD")
{
if(now==mark)
cout<<"Ignored"< else
cout< }
n++;
}
return 0;
}


浏览 36
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报