Little Monkey Programming C++ | Browser

Learn C++ from a young age! Record the questions in the learning process of Xueersi Monkey programming and record every moment. Any infringement will be deleted immediately, thank you for your support!

Attached is a summary post: Little Monkey Programming C++ | Summary-CSDN Blog


[Title description]

In order to improve his strength, Xiaohou often answers questions on major websites. Then he discovered that the browser has three operations: access, forward and back. The little monkey was very curious about the working principles of these three operations. After searching for relevant information on the Internet and combining it with his own practice, he found that their working principles are as follows:

Access : The access operation requires the user to provide a URL. The browser will access the URL and set the predecessor of the URL as the URL before the jump.

Back : The back operation checks whether the current URL has a predecessor (every URL except the first URL has a predecessor). If so, the browser will visit the predecessor of the current URL.

Forward : Used to offset the backward movement during forward operation:

  • If the previous step is to go back, the browser returns to the web page before going back.
  • If the previous step is access, ignore this forward operation (that is, the forward button is not gray and cannot be clicked).
  • If the previous step is also forward, and if the previous step forward has offset a backward step, the forward effect will be determined based on the earlier operation, otherwise, this operation will be ignored.

Now the little monkey is given  n  operations. Please simulate the behavior of the browser and output the URL it visits when performing each operation.

【enter】

The first line contains an integer  n .

Next  n  lines, each line represents a step of operation:

  • Access operation: Start with the character V, followed by a URL, ensuring that there are no spaces or other invisible characters in the middle of the URL.
  • Back operation: represented by character B.
  • Forward operation: represented by character F.

【Output】

A total of  n  lines, for each step:

  • If the browser accesses a certain URL, the URL is output;
  • If the browser ignores this step, it outputs a ?.

【Input sample】

10
V noi.cn
V xiaohoucode.com
V baidu.com
B
B
F
F
F
V gesp.ccf.org.cn
F

【Output sample】

noi.cn
xiaohoucode.com
baidu.com
xiaohoucode.com
noi.cn
xiaohoucode.com
baidu.com
?
gesp.ccf.org.cn
?

[Detailed code explanation]

#include <bits/stdc++.h>
#include <stack>
using namespace std;
int n;
stack<string> A, B;
int main()
{
    cin >> n;
    for (int i=1; i<=n; i++) {
        char op;
        string s;
        cin >> op;
        if (op == 'V') {
            cin >> s;
            A.push(s);
            cout << s << endl;
            while (B.size()>0) B.pop();
        } else if (op=='B') {
            if (A.size()<2) cout << "?" << endl;
            else {
                B.push(A.top());
                A.pop();
                cout << A.top() << endl;
            }
        } else {
            if (B.size()==0) cout << "?" << endl;
            else {
                A.push(B.top());
                B.pop();
                cout << A.top() << endl;
            }
        }
    }
    return 0;
}

【operation result】

10
V noi.cn
noi.cn
V xiaohoucode.com
xiaohoucode.com
V baidu.com
baidu.com
B
xiaohoucode.com
B
noi.cn
F
xiaohoucode.com
F
baidu.com
F
?
V gesp.ccf.org.cn
gesp.ccf.org.cn
F
?

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/134066733