题目链接:http://poj.org/problem?id=1028
Web Navigation
Time Limit: 1000MS | Memory Limit: 10000K |
Total Submissions: 25407 | Accepted: 11317 |
Description
Standard webbrowsers contain features to move backward and forward among the pages recentlyvisited. One way to implement these features is to use two stacks to keep trackof the pages that can be reached by moving backward and forward. In thisproblem, 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 fromthe top of the backward stack, making it the new current page. If the backwardstack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the pagefrom the top of the forward stack, making it the new current page. If theforward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and makethe 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 URLhttp://www.acm.org/
Input
Input is asequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are allin uppercase. URLs have no whitespace and have at most 70 characters. You mayassume that no problem instance requires more than 100 elements in each stackat any time. The end of input is indicated by the QUIT command.
Output
For each commandother than QUIT, print the URL of the current page after the command isexecuted if the command is not ignored. Otherwise, print "Ignored".The output for each command should be printed on its own line. No output isproduced 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
题意:就像浏览器的前进,后退,浏览新页面的功能,用2个栈实现,一个存放前进的页面,一个存放后退的页面
#include <iostream>
#include<stdio.h>
#include<stack>
#include<string>
#include<string.h>
using namespace std;
struct strWeb
{
char str[110];
};
stack<string> sIn;
stack<string> sOut;
int main()
{
string strInit = "http://www.acm.org/";
string ERROR = "Ignored";
sIn.push(strInit);
while(true)
{
string operate;
cin>>operate;
if(operate[0]=='Q')
return 0;
else if(operate[0]=='V')
{//访问网址
string url;
cin>>url;
sIn.push(url);
/*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. */
while(!sOut.empty())
sOut.pop();
cout<<sIn.top()<<endl;
}
else if(operate[0]=='F')
{//前进
if(sOut.size()>0)
{
sIn.push(sOut.top());
sOut.pop();
cout<<sIn.top()<<endl;
}
else
{
cout<<ERROR<<endl;
}
}
else if(operate[0]=='B')
{//回退
if(sIn.size()>1)
{//第一个值不回退
sOut.push(sIn.top());
sIn.pop();
cout<<sIn.top()<<endl;
}
else
{
cout<<ERROR<<endl;
}
}
}
return 0;
}