Web Navigation_poj1028_模拟

来源:互联网 发布:windows exp导出数据库 编辑:程序博客网 时间:2024/06/08 11:36

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.

Analysis


再一次体会到IO流的强大之处
听说复赛提高考模拟频率很高啊(只是在找理由刷水题)
直接两个栈无脑模拟就可以了

Code


#include <iostream>#include <string.h>#include <stack>using namespace std;stack<string>f;stack<string>b;string s,c;int main(){    string now="http://www.acm.org/";    while (cin>>s&&s!="QUIT")    {        if (s=="VISIT")        {            cin>>c;            while (!f.empty())                f.pop();            b.push(now);            now=c;            cout<<now<<endl;        }        if (s=="BACK")        {            if (b.size())            {                f.push(now);                now=b.top();                b.pop();                cout<<now<<endl;            }            else                {                    cout<<"Ignored"<<endl;                }        }        if (s=="FORWARD")        {            if (f.size())            {                b.push(now);                now=f.top();                f.pop();                cout<<now<<endl;            }            else                {                    cout<<"Ignored"<<endl;                }        }    }    return 0;}
0 0
原创粉丝点击