Web Navigation
题目描述:
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/
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.“VISIT”新添加一个网页为当前网页,将原来的当前网页加入backward 栈中,并且清空forward栈。
2.“BACK”把当前网页加入forward栈中,并且取出backward栈的顶部网页作为当前网页。
3.“FORWARD”把当前网页加入backward栈中,并且取出forward栈的顶部网页作为当前网页。
4.判断当执行“BACK”和“FORWARD”操作时,假如栈中没有了网页,则输出“Ignored”。
剩下的就是模拟。
/*************************************************************************
> File Name: Web_Navigation.cpp
> Author: Frade
> Mail: frade@vip.sina.com.cn
> Created Time: 2017年05月10日 星期三 15时05分57秒
************************************************************************/
#include <iostream>
#include <cstdio>
using namespace std;
int Getint()
{
char c = getchar();
bool flag = false;
int ans = 0;
while(c != '-' && (c <'0' || c > '9'))c = getchar();
if(c == '-')flag = true,c = getchar();
while(c >= '0' && c <= '9')ans = ans*10 + c - '0',c = getchar();
return (flag == true) ? -ans : ans;
}
string s1 = "VISIT";
string s2 = "BACK";
string s3 = "FORWARD";
struct Stack
{
string s[105];
int top;
void Init(){top = 0;}
void Push(string key){s[++top] = key;}
string Top(){return s[top];}
void Pop(){top--;}
bool Empty(){return (top == 0);}
}S1,S2;
string Now_web = "http://www.acm.org/";
void Search_Stack()
{
for(int i = S1.top;i >= 1; i--)
{
cout << S1.s[i] << endl;
}
}
void visit()
{
S1.Push(Now_web);
string New_web;
cin >> New_web;
Now_web = New_web;
cout << Now_web << endl;
S2.Init();
}
void back()
{
if(S1.Empty())cout << "Ignored" << endl;
else
{
S2.Push(Now_web);
Now_web = S1.Top();
S1.Pop();
cout << Now_web << endl;
}
}
void forward()
{
if(S2.Empty())cout << "Ignored" << endl;
else
{
S1.Push(Now_web);
Now_web = S2.Top();
S2.Pop();
cout << Now_web << endl;
}
}
int main()
{
string s;
while(true)
{
cin >> s;
if(s == s1)visit();
else if(s == s2)back();
else if(s == s3)forward();
else break;
/*cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
Search_Stack();
cout << ">>>>>>>>>>>>>>>>>>>>>>>>>>>>" << endl;*/
}
return 0;
}