POJ1028 Web Navigation

题目来源:http://poj.org/problem?id=1028

题目大意:

  模拟实现一个浏览器的“前进”和“回退”功能。由一个forward stack和一个backward stack实现。

  打开浏览器时的正位于http://www.acm.org/。然后浏览器会接受下面四种命令:

  BACK:将当前页面压入forward stack,将backward stack顶部页面弹出,成为当前页面。若当前backward stack为空,忽略该命令;

  FORWARD:将当前页面压入backward stack,将forward stack顶部的页面弹出,作为当前页面。若当前forward stack为空,忽略该命令;

  VISIT:将当前页面压入backward stack,将指定的URL作为当前页面;

  QUIT:退出浏览器。

输入:命令的序列,以QUIT作为结束。

输出:如果当前页面发生了改变,输出改变后的当前页面,如果命令被忽略,输出“Ignored.”


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 //////////////////////////////////////////////////////////////////////////
 2 //        POJ1028 Web Navigation
 3 //        Memory: 264K        Time: 47MS
 4 //        Language: C++        Result: Accepted
 5 //////////////////////////////////////////////////////////////////////////
 6 
 7 #include <iostream>
 8 #include <string>
 9 #include <stack>
10 
11 using namespace std;
12 
13 stack<string> forwardStack;
14 stack<string> backwardStack; 
15 
16 void clearForwardStack() {
17     while (!forwardStack.empty()) {
18         forwardStack.pop();
19     }
20 }
21 
22 int main() {
23     string operation;
24     string currentURL = "http://www.acm.org/";
25     string nextURL;
26     while (cin >> operation && operation != "QUIT") {
27 
28         switch (operation[0]) {
29             case 'V':
30                 cin >> nextURL;
31                 backwardStack.push(currentURL);
32                 currentURL = nextURL;
33                 cout << currentURL << endl;
34                 clearForwardStack();
35                 break;
36             case 'B':
37                 if (backwardStack.empty()) {
38                     cout << "Ignored" << endl;
39                 } else {
40                     forwardStack.push(currentURL);
41                     currentURL = backwardStack.top();
42                     cout << currentURL << endl;
43                     backwardStack.pop();
44                 }
45                 break;
46             case 'F':
47                 if (forwardStack.empty()) {
48                     cout << "Ignored" << endl;
49                 } else {
50                     backwardStack.push(currentURL);
51                     currentURL = forwardStack.top();
52                     cout << currentURL << endl;
53                     forwardStack.pop();
54                 }
55                 break;
56         }
57     }
58     system("pause");
59     return 0;
60 }
View Code
posted @ 2013-07-31 22:48  小菜刷题史  阅读(234)  评论(1编辑  收藏  举报