ACM:网页导航

ACM: Web Navigation

本文关键字:导航 网页 ACM      更新时间:2023-10-16

注意:我写的这个问题只是为了解ACM问题的人准备的。
我对这个问题有问题。我为此写了一个很好的解决方案,但每次发送时都会得到错误的答案。我不知道这里出了什么问题。我针对许多测试用例测试了此代码。你能帮我修复我的代码吗?
这是问题的链接:http://sharecode.ir/section/problemset/problem/1022。
这是我的代码:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        string page[1000] = { "" };
        int cntr = 0;
        page[cntr] = "http://www.acm.org/";
        string page1;
        while (1)
        {
            cin >> page1;
            if (page1 == "QUIT")
                break;
            if (page1 == "VISIT")
            {
                cntr++;
                cin >> page1;
                page[cntr] = page1;
                cout << page1 << endl;
            }
            if (page1 == "BACK")
            {
                cntr--;
                if (cntr >= 0)
                    cout << page[cntr] << endl;
                else
                {
                    cout << "Ignored" << endl;
                    cntr = 0;
                }
            }
            if (page1 == "FORWARD")
            {
                cntr++;
                if (page[cntr] == "")
                    cout << "Ignored" << endl;
                else
                    cout << page[cntr] << endl;
            }
        }
        if (n) cout << endl;
    }
}

访问网站后不会清除转发。这就是为什么你会得到错误的答案。

这是正确的代码:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int n){
    cin >> n;
    while (n--){
        string c;
        vector <string> a(999,"");
        a[0] = "http://www.acm.org/";
        int i = 0;
        while(cin>>c,c != "QUIT"){
            if (c == "VISIT"){
                i++;
                string s;
                cin >> s;
                a[i] = s;
                cout << a[i] << "n";
                int t = i+1;
                while (a[t] != "") {
                    a[t] = "";
                    t++;
                }
            }
            if (c == "BACK"){
                i--;
                if (i < 0) {cout << "Ignoredn"; i=0;} else cout << a[i] << "n";
            }
            if (c == "FORWARD"){
                i++;
                if (a[i] == "") {cout << "Ignoredn"; i--;} else cout << a[i] << "n"; 
            }
        }
        if (n) cout << "n";
    }
}