Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15001 | Accepted: 6552 |
Description
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
Output
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
Source
标准的网页浏览器包含在最近浏览的网页中前进和后退的功能。实现该功能的一种方法是利用两个“页面”栈来实现向前以及向后移动。在这个问题中,要求你实现这些。
需要支持如下命令:
BACK:把当前页面放入“前进栈”的最顶部,并从“后退栈”中取出顶部元素,使其为当前浏览页面。如果“后退栈”为空,则忽略当前操作。
FORWARD:把当前页面放入“后退栈”的最顶部,并从“前进栈”中取出顶部元素,使其为当前浏览页面。如果“前进栈”为空,则忽略当前操作。
VISIT :将当前页面放入后退栈的顶部,并设置URL为当前指定页面。前进栈设置为空。
QUIT:退出浏览器。
假设浏览器初始登入URL网页:http://www.acm.org/。
输入:
输入是一系列命令。命令的用大写字母表示的关键字:BACK、FORWARD、VISIT,以及QUIT。URL没有空白并且至多有70个字符。你可以假设在每一个栈中至多存在100个元素。以QUIT命令指明输入结束。
输入:
#include <vector>
#include <string>
#include <fstream>
{
// ifstream cin("test.txt");
string str,temp,current="http://www.acm.org/";
BACK.clear();
FORWARD.clear();
{
if( str=="QUIT" ) // 退出
{
break;
}
else if( str=="BACK" ) // 后退
{
if( BACK.empty()==true ) // 如果后退栈为空 不响应
{
cout<<"Ignored"<<endl;
continue;
}
else
{
FORWARD.push_back(current);
current = BACK[BACK.size()-1];
BACK.pop_back();
}
}
else if( str=="FORWARD" ) // 前进
{
if( FORWARD.empty()==true ) // 如果前进栈为空 不响应
{
cout<<"Ignored"<<endl;
continue;
}
else
{
BACK.push_back(current);
current = FORWARD[ FORWARD.size()-1 ];
FORWARD.pop_back();
}
}
else // 访问
{
cin>>temp;
BACK.push_back(current);
current = temp;
FORWARD.clear();
}
}
return 0;
}