ACM:Web导航

时间:2013-12-22 14:49:48

标签: c++

注意:我写的这个问题仅适用于了解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;
    }
}

1 个答案:

答案 0 :(得分:1)

访问网站后,您无法清除转发。这就是你得错答案的原因。

这是正确的代码:

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(int n){
    cin >> n;
    while (n--){
        string c;
        vector <string> a(999,"\0");
        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] != "\0") {
                    a[t] = "\0";
                    t++;
                }

            }
            if (c == "BACK"){
                i--;
                if (i < 0) {cout << "Ignored\n"; i=0;} else cout << a[i] << "\n";
            }
            if (c == "FORWARD"){
                i++;
                if (a[i] == "\0") {cout << "Ignored\n"; i--;} else cout << a[i] << "\n"; 
            }
        }
        if (n) cout << "\n";
    }

}