POJ Web Navigation

来源:互联网 发布:快速开方算法 编辑:程序博客网 时间:2024/05/16 09:36
Web Navigation
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 15001 Accepted: 6552

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/

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/BACKBACKBACKFORWARDVISIT http://www.ibm.com/BACKBACKFORWARDFORWARDFORWARDQUIT

Sample Output

http://acm.ashland.edu/http://acm.baylor.edu/acmicpc/http://acm.ashland.edu/http://www.acm.org/Ignoredhttp://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

East Central North America 2001
译文:

标准的网页浏览器包含在最近浏览的网页中前进和后退的功能。实现该功能的一种方法是利用两个“页面”栈来实现向前以及向后移动。在这个问题中,要求你实现这些。

需要支持如下命令:

BACK把当前页面放入“前进栈”的最顶部,并从“后退栈”中取出顶部元素,使其为当前浏览页面。如果“后退栈”为空,则忽略当前操作。

FORWARD:把当前页面放入“后退栈”的最顶部,并从“前进栈”中取出顶部元素,使其为当前浏览页面。如果“前进栈”为空,则忽略当前操作。

VISIT :将当前页面放入后退栈的顶部,并设置URL为当前指定页面。前进栈设置为空。

QUIT:退出浏览器。

假设浏览器初始登入URL网页:http://www.acm.org/

输入:

       输入是一系列命令。命令的用大写字母表示的关键字:BACKFORWARDVISIT,以及QUITURL没有空白并且至多有70个字符。你可以假设在每一个栈中至多存在100个元素。以QUIT命令指明输入结束。

输入:

       除了QUIT命令,输出每一个命令执行后对应的当前页面的URL,如果当前命令没有被执行则输出“Ignored”。每个命令的输出都各占一行。当执行命令QUIT时,没有输出。
我的思路:
    这题只要读懂题给的要求,基本就不难啦。我采用vector 来充当栈的作用,使用简单。
我的代码:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
vector<string> BACK, FORWARD;             // 定义后退栈 以及前进栈
int main()
{
// ifstream cin("test.txt");
 string str,temp,current="http://www.acm.org/";
 BACK.clear();
 FORWARD.clear();
 while( cin>>str )
 {
  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();
  }
  cout<<current<<endl;
 }
 return 0;
}
原创粉丝点击