zoukankan      html  css  js  c++  java
  • 计蒜客 网页跳转(多个栈)

    题目要求:

    蒜头君每天都在用一款名为“蒜厂浏览器”的软件。在这个浏览器中,一共三种操作:打开页面、回退和前进。它们的功能如下:

    打开页面:在地址栏中输入网址,并跳转到网址对应的页面;

    • 回退:返回到上一次访问的页面;
    • 前进:返回到上次回退前的页面,如果上一次操作是打开页面,那么将无法前进。
    • 现在,蒜头君打开浏览器,进行了一系列操作,你需要输出他每次操作后所在页面的网址。

    输入格式

    第一行输入一个整数 n(0 < n le 100000)n(0<n≤100000),表示蒜头君的操作次数。

    接下来一共 nn 行,每行首先输入一个字符串,如果是VISIT,后面接着输入一个不含有空格和换行的网址(网址长度小于 100100),表示蒜头君在浏览器地址栏中输入的网址;如果是BACK,表示蒜头君点击了回退按钮;如果是FORWARD,表示蒜头君点击了前进按钮。

    输出格式

    对于每次操作,如果蒜头君能操作成功,输出蒜头君操作之后的网址,否则输出Ignore。假设蒜头君输入的所有网址都是合法的。

    样例输入

    10
    VISIT https://www.jisuanke.com/course/476
    VISIT https://www.taobao.com/
    BACK
    BACK
    FORWARD
    FORWARD
    BACK
    VISIT https://www.jisuanke.com/course/429
    FORWARD
    BACK

    样例输出

    https://www.jisuanke.com/course/476
    https://www.taobao.com/
    https://www.jisuanke.com/course/476
    Ignore
    https://www.taobao.com/
    Ignore
    https://www.jisuanke.com/course/476
    https://www.jisuanke.com/course/429
    Ignore
    https://www.jisuanke.com/course/476

    思路:https://blog.csdn.net/weixin_44336954/article/details/92025250

    代码源自:https://blog.csdn.net/biubiupa233/article/details/79521058

     1 #include <iostream>
     2 #include <stack>
     3 #include <string>
     4 using namespace std;
     5 int main() {
     6     ios::sync_with_stdio(0);//避免输入输出超时 
     7     stack<string> s1;//存放后退的网站 
     8     stack<string> s2;//存放前进的网站 
     9     int n;
    10     cin >> n;
    11     while (n--) {
    12         string str1, str2;
    13         cin >> str1;
    14         if (str1 == "VISIT") { 
    15             cin >> str2;
    16             while (!s2.empty()) //如果有访问就清空前进的网站 
    17                 s2.pop();
    18             s1.push(str2);//后退的网站中存放进当前访问的网站,
    19                           //等要后退的时候可以提取这个网站放入前进的网站中去 
    20             cout << s1.top() << endl;
    21         }
    22         else if (str1 == "BACK") {
    23             if (s1.empty()) cout << "Ignore" << endl; //如果要后退的网站的栈为空 
    24             else {//不为空 
    25                 s2.push(s1.top());//把之前访问的网站,存进前进的网站的栈中 
    26                 s1.pop();
    27                 if (!s1.empty()) cout << s1.top() << endl;//后退网站的栈不为空则输出 
    28                 else {//为空
    29                     s1.push(s2.top());//返回之前的存放状态 
    30                     s2.pop();
    31                     cout << "Ignore" << endl;
    32                 }
    33             }
    34         }
    35         else {
    36             if (s2.empty())//前进为空 
    37                 cout << "Ignore" << endl;
    38             else {//不为空 
    39                 cout << s2.top() << endl;
    40                 s1.push(s2.top());
    41                 s2.pop();    
    42             }
    43         }
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    把影响集中到一个点
    How to avoid Over-fitting using Regularization?
    适定性问题
    Numerical Differentiation 数值微分
    What Every Computer Scientist Should Know About Floating-Point Arithmetic
    Generally a good method to avoid this is to randomly shuffle the data prior to each epoch of training.
    What is the difference between iterations and epochs in Convolution neural networks?
    Every norm is a convex function
    Moore-Penrose Matrix Inverse 摩尔-彭若斯广义逆 埃尔米特矩阵 Hermitian matrix
    perl 类里的函数调用其他类的函数
  • 原文地址:https://www.cnblogs.com/jiamian/p/12164579.html
Copyright © 2011-2022 走看看