关于#ar#的问题,如何解决?(语言-c++)

上网

时间:0.2s 空间:32M

题目描述:
现在三种浏览网页的行为,跟我们平时上网情景一样

**visit: **访问某个特定的网址,相当于我们在当前页面的地址栏输入一个url

back: 回到上一次访问的网址,相当于下图中的左箭头

**forward: **相当于下图中的右箭头。

要注意,一旦visit操作之后,右箭头就会消失,因为刚才输入的网址已经是最新网址,没有更新的了,可以自己试试再来做此题。

假设一开始正在访问的第一个网址为http://www.hzxjhs.com/

输入格式:
输入若干行,碰到"QUIT"结束,表示关闭浏览器

其他一共三种输入类型。

VISIT 操作后面会跟一个字符串,表示网址

BACK

FORWARD

输出格式:
对于每个操作,输出当前访问的网址,若操作非法,输出"Ignored"

样例输入:
VISIT http://www.codeforces.com/
VISIT http://www.topcoder.com/
BACK
BACK
BACK
FORWARD
VISIT http://www.codechef.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT
样例输出
http://www.codeforces.com/
http://www.topcoder.com/
http://www.codeforces.com/
http://www.hzxjhs.com/
Ignored
http://www.codeforces.com/
http://www.codechef.com/
http://www.codeforces.com/
http://www.hzxjhs.com/
http://www.codeforces.com/
http://www.codechef.com/
Ignored

约定:
所有的网址都没有空格,最长的网址为70

总的操作次数不超过1000

原题:XJOI 3317

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 以下回答由chatgpt基于相关博客总结生成:
    #include <iostream>
    #include <string>
    #include <stack>
    
    using namespace std;
    
    int main() {
        string current_url = "http://www.hzxjhs.com/";
        stack<string> back_stack;
        stack<string> forward_stack;
        string command, url;
    
        while (true) {
            cin >> command;
    
            if (command == "QUIT") {
                break;
            }
    
            if (command == "VISIT") {
                cin >> url;
                back_stack.push(current_url);
                current_url = url;
                cout << current_url << endl;
                while (!forward_stack.empty()) {
                    forward_stack.pop();
                }
            } else if (command == "BACK") {
                if (!back_stack.empty()) {
                    forward_stack.push(current_url);
                    current_url = back_stack.top();
                    back_stack.pop();
                    cout << current_url << endl;
                } else {
                    cout << "Ignored" << endl;
                }
            } else if (command == "FORWARD") {
                if (!forward_stack.empty()) {
                    back_stack.push(current_url);
                    current_url = forward_stack.top();
                    forward_stack.pop();
                    cout << current_url << endl;
                } else {
                    cout << "Ignored" << endl;
                }
            }
        }
    
        return 0;
    }
    

    解题思路: 1. 定义一个字符串变量current_url,用于保存当前访问的网址。 2. 定义两个栈back_stackforward_stack,分别用于保存访问历史和前进历史。 3. 不断读入操作指令,根据不同的指令进行相应的操作: - 如果是VISIT指令,将当前网址入栈到back_stack,将指定的网址设为当前网址,并输出该网址。同时将forward_stack清空,因为进行了新的访问。 - 如果是BACK指令,如果back_stack不为空,将当前网址入栈到forward_stack,将back_stack的顶部网址设置为当前网址,并输出该网址。如果back_stack为空,说明已经无法后退,输出"Ignored"。 - 如果是FORWARD指令,如果forward_stack不为空,将当前网址入栈到back_stack,将forward_stack的顶部网址设置为当前网址,并输出该网址。如果forward_stack为空,说明已经无法前进,输出"Ignored"。 4. 当读入到QUIT指令时退出循环,结束程序。

    该解法的时间复杂度为O(N),其中N为操作次数。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^