zoukankan      html  css  js  c++  java
  • Discover the Web(栈模拟)

    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. You are asked to implement this. The commands are:

    1. BACK: If the backward stack is empty, the command is ignored. Otherwise, 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.
    2. FORWARD: If the forward stack is empty, the command is ignored. Otherwise, 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.
    3. VISIT <url>: 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.
    4. QUIT: Quit the browser.

    The browser initially loads the web page at the URL 'http://www.lightoj.com/'

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case contains some commands. The keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 50 characters. The end of case is indicated by the QUIT command and it shouldn't be processed. Each case contains at most 100 lines.

    Output

    For each case, print the case number first. For each command, print the URL of the current page (in a line) after the command is executed if the command is not ignored. Otherwise, print 'Ignored'.

    Sample Input

    1

    VISIT http://uva.onlinejudge.org/

    VISIT http://topcoder.com/

    BACK

    BACK

    BACK

    FORWARD

    VISIT http://acm.sgu.ru/

    BACK

    BACK

    FORWARD

    FORWARD

    FORWARD

    QUIT

    Sample Output

    Case 1:

    http://uva.onlinejudge.org/

    http://topcoder.com/

    http://uva.onlinejudge.org/

    http://www.lightoj.com/

    Ignored

    http://uva.onlinejudge.org/

    http://acm.sgu.ru/

    http://uva.onlinejudge.org/

    http://www.lightoj.com/

    http://uva.onlinejudge.org/

    http://acm.sgu.ru/

    Ignored

    题目意思:利用栈模拟一下浏览器访问网页的过程。

    解题思路:建立两个栈,分别储存向前和向后的元素,其实就是在来回倒。

    唉,好久没做题了,碰上这一道栈的题,来回做了好久,不过也是在补之前的漏洞,之前就碰到过这样一道双栈的题目,然而并没有及时补题,拖到了现在。

     1 #include<iostream>
     2 #include<stack>
     3 #include<algorithm>
     4 #include<stdio.h>
     5 using namespace std;
     6 int main()
     7 {
     8     int t,count=1;
     9     string x,y;
    10     scanf("%d",&t);
    11     while(t--)
    12     {
    13         printf("Case %d:
    ",count++);
    14         stack<string>s1;
    15         stack<string>s2;
    16         s1.push("http://www.lightoj.com/");
    17         while(1)
    18         {
    19             cin>>x;
    20             if(x[0]=='Q')
    21             {
    22                 break;
    23             }
    24             else if(x[0]=='V')
    25             {
    26                 cin>>y;
    27                 s1.push(y);
    28                 cout<<y<<endl;
    29                 while(!s2.empty())///清空
    30                 {
    31                     s2.pop();
    32                 }
    33             }
    34             else if(x[0]=='B')///因为s1栈顶元素是当前访问的页面,后退一步必须返回当前栈顶的下一个元素。
    35             {
    36                 if(s1.size()>1)///此时栈内必须有两个以上的元素
    37                 {
    38                     s2.push(s1.top());
    39                     s1.pop();///删掉当前的页面
    40                     cout<<s1.top()<<endl;
    41                 }
    42                 else
    43                 {
    44                     cout<<"Ignored"<<endl;
    45                 }
    46             }
    47             else if(x[0]=='F')
    48             {
    49                 if(!s2.empty())
    50                 {
    51                     s1.push(s2.top());
    52                     cout<<s2.top()<<endl;
    53                     s2.pop();///删掉当前的页面
    54                 }
    55                 else
    56                 {
    57                     cout<<"Ignored"<<endl;
    58                 }
    59             }
    60         }
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    如何下载Bilibili视频
    pyqt5 主界面打开新主界面、打开Dialog、打开提示框的实现模板
    【爬坑】python3+pyqt5+pyinstaller 打包成exe的各种问题
    【PyQt5-Qt Designer】QComboBox(下拉列表框) 使用模板
    【PyQt5-Qt Designer】读取txt文件在打印
    pyqt5核心-信号与槽(第二弹)
    使用QT设计师-信号和槽signal-slot(第一弹)
    【python基础】python程序打包成.exe运行时会弹出黑框
    【pyqt5】QdateTimeEdit(日期时间)
    pyqt5-对文本样式进行操作
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9085163.html
Copyright © 2011-2022 走看看