您的位置:首页 > 其它

ZOJ Problem Set - 1061

2013-09-01 17:31 381 查看
【模拟题】

【题目】

Web Navigation

Time Limit: 2 Seconds Memory Limit: 65536 KB

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 <url>: 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/
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank
line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.

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
1
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

Source: East Central North America 2001

【题意说明】

在浏览器中基本的网页访问一般都支持三种访问方式:在地址栏里输入URL、后退、前进。现在本题模拟一个简单的浏览器,程序输入包括多组用例,每组用例中输入三种访问网页的操作:(1)直接访问URL(VISIT URL)(2)针对当前网页进行后退(BACK)(3)针对当前网页进行前进(FORWARD),对应每组用例的每个输入,输出当前正在访问的网页URL,如果操作失误导致网页不可达(比如输入URL访问后操作前进访问或者当后退到第一次访问的网页后仍在操作后退访问)则输出"Ignored",每组用例以“QUIT”结束。

【解答】

(一)分析:当输入URL访问或者前进访问时,可把上一次刚访问过的网页URL压入一个后退栈中,则后退栈的栈顶保存的是相对于当前网页之前最近访问过的网页,这样当进行后退操作时便可弹出栈顶网页作为后退操作正在访问的网页;同理,当后退访问时,可把上一次刚访问过的网页URL压入一个前进栈中,则前进栈顶保存的是离后退访问的网页之后的最近网页,这样当进行前进操作时便可弹出栈顶网页作为前进操作正在访问的网页;另外,不难得出,当进行后退或前进操作时,如果后退栈或前进栈为空,则网页不可达。



(二)代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
using namespace std;

int main()
{
int n,num;
stack<string> back,forward;
string url,current;//current记录当前正在访问的网页(由VISIT/BACK/FORWARD触发)
char action[10];
scanf("%d",&n);
//n个用例
for(num=1;num<=n;num++)
{
current="http://www.acm.org/";
//每个用例前清空后退栈、前进栈
while(!back.empty())
back.pop();
while(!forward.empty())
forward.pop();
//输入网页访问动作
while(scanf("%s",action)&&strcmp(action,"QUIT")!=0)
{
//URL访问方式
if(strcmp(action,"VISIT")==0)
{
cin>>url;
back.push(current);//将上次访问的网页压入后退栈备用
current=url;
cout<<current<<endl;//输出当前正在访问的网页
//当输入URL访问过一个网页后,则前进动作无网页,故清空前进栈
while(!forward.empty())
forward.pop();
}
//后退方式
else if(strcmp(action,"BACK")==0)
{
if(!back.empty())
{
forward.push(current);//将之前刚访问的网页压入前进栈备用
url=back.top();
current=url;//当前访问的网页为后退栈顶
back.pop();//取栈顶后删除原栈顶
cout<<current<<endl;//输出当前正在访问的网页
}
else
printf("Ignored\n");
}
//前进方式
else if(strcmp(action,"FORWARD")==0)
{
if(!forward.empty())
{
back.push(current);//将之前刚访问的网页压入后退栈备用
url=forward.top();
current=url;//当前访问的网页为前进栈顶
forward.pop();//取栈顶后删除原栈顶
cout<<current<<endl;//输出当前正在访问的网页
}
else
printf("Ignored\n");
}
}
if(num<n)
printf("\n");
}

return 0;
}
//Accepted


(解于2009/10)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: