题目描述
标准的 Web 浏览器包含在最近访问过的页面中向后和向前移动的功能。实现这些特性的一种方法是使用两个栈来跟踪前后移动可以到达的页面。支持以下命令。
BACK:将当前页面推到前向栈的顶部。从后向栈的顶部弹出页面,使其成为新的当前页面。如果后向栈为空,则忽略该命令。
FORWARD:将当前页面推到后向栈的顶部。从前向栈顶部弹出页面,使其成为新的当前页面。如果前向栈为空,则忽略该命令。
VISIT:将当前页面推到后向栈的顶部,使得 URL 成为新的当前页面,前向栈清空。
QUIT:退出浏览器。
假设浏览器的最初页面为 URL ***###.acm.org/ (对 “http://” 用 “***” 代替,对 “www” 用 “###” 代替)。
输入格式
输入是一系列 BACK、FORWARD、VISIT、QUIT 命令。
输出格式
对于除了 QUIT 外的每个命令,如果不忽略该命令,则在执行该命令后单行输出当前页的 URL,否则输出 “Ignored”。
QUIT 命令没有输出。
输入样例
VISIT ***acm.ashland.edu/
VISIT ***acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT ***###.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
输出样例
***acm.ashland.edu/
***acm.baylor.edu/acmicpc/
***acm.ashland.edu/
***###.acm.org/
Ignored
***acm.ashland.edu/
***###.ibm.com/
***acm.ashland.edu/
***###.acm.org/
***acm.ashland.edu/
***###.ibm.com/
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<string> s1,s2;
string op,web;
web = "***###.acm.org/";
while (cin>>op)
{
if (op == "VISIT")
{
s1.push(web);
cin >> web;
cout << web << endl;
while (!s2.empty())
{
s2.pop();
}
}
else if (op == "BACK")
{
if (s1.empty())
cout << "Ignored" << endl;
else
{
s2.push(web);
web = s1.top();
s1.pop();
cout << web << endl;
}
}
else if (op == "FORWARD")
{
if(s2.empty())
cout << "Ignored" << endl;
else
{
s1.push(web);
web = s2.top();
s2.pop();
cout << web << endl;
}
}
else
{
return 0;
}
}
}