您的位置:首页 > 其它

POJ-1028-Web Navigation解题心得

2010-12-18 15:18 295 查看
这道题是目前为止本人做到的最难的题。这道题只要是练习下stack的使用,另外,对于C++中STL的使用也有一定的涉及。(当然,这些数据结构是可以自己实现的,不过为了节省时间,我直接使用了。)



题意是很容易理解的,就是编程模拟实现浏览器的“前进”,“后退”功能。且题目中已说明了如何使用stack 进行编程,应该不算难的。



解决这道题主要遇到的问题:

1)对于当前URL的初始化,“后退stack“的初始化开始时不是很明确,导致花费了不少时间。

2)本人是在Linux用C++实现的,开始时认为input是一条命令一行,导致自己使用了getline函数,提交时出现了“Wrong Answer",自己也莫明其妙。(因为,自己用题目中给出的测试用例测试时完全与题目中给出的输出结果一致。)后来参考下别人的程序,才知道input理解为只以空格隔开即可。

3)还不太熟悉POJ所用的编译器,现在知道如果用C++编程,选择G++编译器即可。



下面是源代码:

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
    stack<string> backStk;
    stack<string> forStk;
    string aLine;
    string quitStr;
    string url = "http://www.acm.org/";
    while(getline(cin, aLine)) {
       quitStr = aLine.substr(0, 4);
       if(quitStr.compare("QUIT") == 0)
            break;
        if(aLine.find("VISIT") != string::npos) {
            backStk.push(url);
            string::size_type httpPos = aLine.find("http://");
            url = aLine.substr(httpPos);
            cout << url << endl;
            while(forStk.empty() == false)
                forStk.pop();
        } else if(aLine.find("BACK") != string::npos) {
            if(backStk.empty() == true) {
                cout << "Ignored" << endl;
                continue;
            }
            forStk.push(url);
            url = backStk.top();
            backStk.pop();
            cout << url << endl;
        } else if(aLine.find("FORWARD") != string::npos) {
            if(forStk.empty() == true) {
                cout << "Ignored" << endl;
                continue;
            }
            backStk.push(url);
            url = forStk.top();
            forStk.pop();
            cout << url << endl;
        }
    }
    return 0;
}


上面的代码假定了输出以行为单位,这个程序在POJ上WA。但实际上已经能够正确解决题目的问题。



下面则是可以通过的代码:

#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
    stack<string> backStk;
    stack<string> forStk;
    string operation;
    string url = "http://www.acm.org/";
    while(cin >> operation) {
       if(operation.compare("QUIT") == 0)
            break;
        if(operation.compare("VISIT") == 0) {
            backStk.push(url);
            cin >> url;
            cout << url << endl;
            while(forStk.empty() == false)
                forStk.pop();
        } else if(operation.compare("BACK") == 0) {
            if(backStk.empty() == true) {
                cout << "Ignored" << endl;
                continue;
            }
            forStk.push(url);
            url = backStk.top();
            backStk.pop();
            cout << url << endl;
        } else if(operation.compare("FORWARD") == 0) {
            if(forStk.empty() == true) {
                cout << "Ignored" << endl;
                continue;
            }
            backStk.push(url);
            url = forStk.top();
            forStk.pop();
            cout << url << endl;
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: