hnu 暑期实训之web导航

题目

【问题描述】

标准的Web浏览器具有在最近访问的页面中前后移动的特性。实现这些特性的一种方法是使用两个堆栈来跟踪可以通过前后移动到达的页面。在这个问题中,我们要求实现这一点。

   需要支持以下命令:

   BACK:将当前页面压入前向堆栈的顶部;从后向堆栈的顶部弹出该页,使其成为新的当前页。如果后向堆栈为空,则该指令忽略。

   FORWARD:将当前页面压入后向堆栈的顶部;从前向堆栈的顶部弹出该页,使其成为新的当前页。如果前向堆栈为空,则该指令忽略。

   VISIT:将当前页面压入后向堆栈的顶部,将URL指定为新的当前页。前向堆栈被清空。

   QUIT:退出浏览器。

   假设浏览器最初在网址http://www.game.org/上加载网页。

输入形式】输入是一个命令序列。命令关键字BACK、FORWARD、VISIT和QUIT都是大写。URL中无空格,最多有70个字符。假定在任何时候,每个堆栈中没有问题实例需要超过100个元素。输入的结尾由QUIT命令标识。

【输出形式】除QUIT外的每个命令,如果命令没有被忽略,则在命令执行后输出当前页面的URL,否则,打印"Ignored"。每个命令的输出独立打印一行。QUIT命令无输出。

【样例输入】

VISIT http://game.ashland.edu/
VISIT http://game.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.our.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

【样例输出】

http://game.ashland.edu/

http://game.baylor.edu/acmicpc/

http://game.ashland.edu/

http://www.game.org/

Ignored

http://game.ashland.edu/

http://www.our.com/

http://game.ashland.edu/

http://www.game.org/

http://game.ashland.edu/

http://www.our.com/

Ignored

思路

利用两个栈就行了,注意模拟过程的正确性,题目本身不难。栈没有clear这个函数,如果需要把栈清空的话,要用下面这个代码。

stack<int> s;//初始化一个存放int的栈
while(!s.empty()){
	s.pop();
}

AC代码

#include <bits/stdc++.h>
using namespace std;

stack<string> front, back; //代表前向堆栈与后向堆栈
int main() {
	string ask;//表示访问url
	string order;//表示命令
	string url = "http://www.game.org/";
	front.push(url);
	while (cin >> order, order != "QUIT") {
		if (order == "VISIT") {
			cin >> ask;
			back.push(url);
			url = ask; //更换url
			while (!front.empty()) {
				front.pop();
			}
			cout << url << endl;
		} else if (order == "BACK") {
			if (back.empty()) { //如果后向堆栈是空的 该条命令被忽视
				cout << "Ignored" << endl;
			} else {
				front.push(url);
				url = back.top();
				back.pop();
				cout << url << endl;
			}
		} else if (order == "FORWARD") {
			if (front.empty()) {
				cout << "Ignored" << endl;
			} else {
				back.push(url);
				url = front.top();
				front.pop();
				cout << url << endl;
			}
		}
	}
}

python版

主要在于stack的导入,python中是没有stack这个数据结构的,我们使用的先进后出队列(LifoQueue)来实现stack的功能的。

from queue import LifoQueue
front=LifoQueue(maxsize=0)
back=LifoQueue(maxsize=0)
#定义两个先进后出队列
url='http://www.game.org/'
while(True):
    s=input()
    if(s=="QUIT"): break
    elif ' ' in s:
        order,url1=s.split() #如果s里面有空格那么说明是VISIT
        back.put(url)
        url=url1
        print(url)
        while(front.qsize()!=0):
            a=front.get() 
        #弹出前向堆栈中的数 有返回值的
    elif s=='BACK':
        if(back.qsize()==0):
            print("Ignored")
        else:
            front.put(url)
            url=back.get()
            print(url)
    elif s=='FORWARD':
        if(front.qsize()==0):
            print('Ignored')
        else :
            back.put(url)
            url=front.get()
            print(url)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一袍清酒付825

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值