Web Navigation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35451 | Accepted: 15869 |
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/
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
【思路】需要两个栈,一个用于存放后退的页面,一个用于存放前进的页面。同时本题有多个注意点:
(1)当你输入一个URL地址时,就一定有后退的页面,同时肯定没有前进的页面。
(2)当你点击后退时候肯定会产生前进的页面,同理前进时候肯定会产生后退的页面。
(3)嗯...其他的大概备注在程序里了。
【代码】AC的C++代码如下:
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
/* 注意事项:
访问一个URL后就没有前进的页面,但是有后退的页面。
后退后就产生前进的页面。
前进完会产生后退的页面。
*/
int main()
{
stack<string> stack1,stack2;
string s,url;
string init_page = "http://www.acm.org/";
stack1.push(init_page);
while (cin >> s)
{
if (s == "QUIT")
break;
else if (s == "BACK")
{
//若有两个以上元素,则最顶端为当前访问的页面,需要先弹出,然后下一个为返回后的页面
if (stack1.size() >= 2) //只有两个以上的URL存在时,返回键才有效
{
stack2.push(stack1.top());
stack1.pop(); //先弹出当前路径,输出下一个路径
cout << stack1.top() << endl;
}
//若只有一个元素,则当前位置已经为初始页面没法返回。
else
{
cout << "Ignored" << endl;
}
}
//访问一个URL后就没有前进的页面,需要将stack2清空
else if (s == "VISIT")
{
cin >> url;
stack1.push(url);
cout << url << endl;
while (!stack2.empty())
stack2.pop();
}
/*
stack1 : c b a (top)
back : a -> b -> c stack2 : a b c (top)
forward : c -> b -> a
*/
else if (s == "FORWARD")
{
if (!stack2.empty())
{
//前进完会产生后退的页面
stack1.push(stack2.top());
cout << stack2.top() << endl;
stack2.pop();
}
else
{
cout << "Ignored" << endl;
}
}
else
{
}
}
return 0;
}