POJ 1028 小细节没处理好,没想好

[NWPU2015][SRM][02][div2]三一五专场
2:00:00
B - B
Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u

Description

Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this.
The following commands need to be supported:
BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored.
FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored.
VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied.
QUIT: Quit the browser.
Assume that the browser initially loads the web page at the URL http://www.acm.org/

Input

Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command.

Output

For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command.

Sample Input

VISIT http://acm.ashland.edu/
VISIT http://acm.baylor.edu/acmicpc/
BACK
BACK
BACK
FORWARD
VISIT http://www.ibm.com/
BACK
BACK
FORWARD
FORWARD
FORWARD
QUIT

Sample Output

http://acm.ashland.edu/
http://acm.baylor.edu/acmicpc/
http://acm.ashland.edu/
http://www.acm.org/
Ignored
http://acm.ashland.edu/
http://www.ibm.com/
http://acm.ashland.edu/
http://www.acm.org/
http://acm.ashland.edu/
http://www.ibm.com/
Ignored


先贴上回宿宿舍时ac的代码,有点乱,以后重写

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:B
题    意:
解题思路:此题我用的数组模拟,看被人的用两个stack貌似也行,
更简单?有时间时试试
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1100000;
const int INF = 2100000000;



int main()
{
    //freopen("input.txt", "r", stdin);
    vector<string> v;
    string command, site;
    v.push_back("http://www.acm.org/");
    int pos = 0;
    while(cin >> command)
    {
//        for(int i = 0; i < v.size(); i++)
//        {
//            cout<< "->" << v[i] << "->" << endl;;
//        }
//        cout << endl;
        if(command == "QUIT")
            break;
        if(command == "VISIT")
        {
            vector<string>::iterator it = v.begin();
            cin >> site;
            pos++;
            v.insert(it + pos, site);
            cout << site << endl;

            /*就是这里啊,先wa然后是Runtime error,一个半小时就卡在这道
            提上了,回宿舍一打开程序,没过十分钟就发现问题了,真是。。。
            看来以后比赛考场上有暂时有点思路,样例能对的题但就是过不了的,
            一定要先放放啊,不要跟一道题死较劲这样必死无疑啊,先放放这道题,
            看看其他的题,然后有时间在回头看才是对的,不然可能结果会很惨*/
            /*
            这里我的问题是刚考试用的vector<string>::iterator it = v.begin();
            这一句,然后以后每次用vector v的起始地址时就用了it,但是赛后发现
            正是这里错了,因为我每次insert时接着有可能要eraser,之后地址可能
            发生变化,所以当把it换成v.begin()是就ac了,。。。。,一夏两种方式
            都是可行的,比赛时改了好多种写法,却是没有用v.begin()导致一直错啊
            。总之,就是vector的大小(存储的值)一直在变,他的首地址可能变化,
            所以每次都要用v.begin()才是对的;
            */
//           while((v.begin()+pos+1) != v.end())
//           {
//               v.erase(v.begin()+pos+1);
//           }
            if(pos != (v.size()-1))
            {
                for(it = it+pos +1;it=v.begin(),it = it+pos +1,it != v.end(); it++)
                {
                    v.erase(it);
                }
            }
            //v.erase(it+pos+1, (it+pos+(v.size()-pos)-1));
        }
        if(command == "BACK")
        {
            //cout << "-->"<<pos << endl;
            if(pos == 0)
                cout << "Ignored" << endl;
            else
            {
                pos--;
                //cout << "->" << pos << endl;
                cout << v[pos] << endl;
            }
        }
        if(command == "FORWARD")
        {
            if(pos == v.size()-1)
                cout << "Ignored" << endl;
            else
            {
                pos++;
                cout << v[pos] << endl;
            }
        }
    }
    return 0;
}

比赛时交了两次都是RE,比如这次提交的

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:B
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1100000;
const int INF = 2100000000;

vector<string> v;
string command, site;

int main()
{
    //freopen("input.txt", "r", stdin);
    v.push_back("http://www.acm.org/");
    int pos = 0;
    while(cin >> command)
    {
//        for(int i = 0; i < v.size(); i++)
//        {
//            cout<< "->" << v[i] << "->" << endl;;
//        }
//        cout << endl;
        if(command == "QUIT")
            break;
        if(command == "VISIT")
        {
            vector<string>::iterator it = v.begin();
            cin >> site;
            v.insert(it + pos+1, site);
            pos++;
            cout << site <<endl;
            if(pos != (v.size()-1))
            v.erase(it+pos+1, (it+pos+(v.size()-pos )));
        }
        if(command == "BACK")
        {
            //cout << "-->"<<pos << endl;
            if(pos == 0)
                cout << "Ignored" << endl;
            else
            {
                pos--;
                //cout << "->" << pos << endl;
                cout << v[pos] << endl;
            }
        }
        if(command == "FORWARD")
        {
            if(pos == v.size()-1)
                cout << "Ignored" << endl;
            else
            {
                pos++;
                cout << v[pos] << endl;
            }
        }
    }
    return 0;
}

然后稍作改动ac = =

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:B
题    意:
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1100000;
const int INF = 2100000000;

vector<string> v;
string command, site;

int main()
{
    freopen("input.txt", "r", stdin);
    v.push_back("http://www.acm.org/");
    int pos = 0;
    while(cin >> command)
    {
//        for(int i = 0; i < v.size(); i++)
//        {
//            cout<< "->" << v[i] << "->" << endl;;
//        }
//        cout << endl;
        if(command == "QUIT")
            break;
        if(command == "VISIT")
        {
            vector<string>::iterator it = v.begin();
            cin >> site;
            v.insert(it + pos+1, site);
            pos++;
            cout << site <<endl;
            if(pos != (v.size()-1))
            //v.erase(it+pos+1, (it+pos+(v.size()-pos )));
            v.erase(v.begin()+pos+1, (v.begin()+pos+(v.size()-pos)));
        }
        if(command == "BACK")
        {
            //cout << "-->"<<pos << endl;
            if(pos == 0)
                cout << "Ignored" << endl;
            else
            {
                pos--;
                //cout << "->" << pos << endl;
                cout << v[pos] << endl;
            }
        }
        if(command == "FORWARD")
        {
            if(pos == v.size()-1)
                cout << "Ignored" << endl;
            else
            {
                pos++;
                cout << v[pos] << endl;
            }
        }
    }
    return 0;
}

重写一遍简洁无注释:

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:B
题    意:重写一遍
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>

using namespace std;
vector<string> v;
string s, site;

int main()
{
    //freopen("input.txt", "r", stdin);
    v.push_back("http://www.acm.org/");
    int pos = 0;//开始时候把pos的位置反倒while下面去了,导致出错了
    while(cin >> s)
    {
        if(s == "QUIT")
        {
            break;
        }
        if(s == "VISIT")
        {
            cin >> site;
            cout << site << endl;
            pos++;
            v.insert(v.begin()+pos, site);
            if(pos != v.size()-1)
            {
                v.erase(v.begin()+pos+1, v.end());
            }
        }
        if(s == "BACK")
        {
            if(pos == 0) cout << "Ignored" << endl;
            else
            {
                pos--;
                cout << v[pos] << endl;
            }
        }
        if(s == "FORWARD")
        {
            if(pos == v.size()-1) cout << "Ignored" << endl;
            else
            {
                pos++;
                cout << v[pos] << endl;
            }
        }
    }
    return 0;
}


方法二:两个栈模拟,前面是vector数组模拟的栈,用两个栈也可以的。

//@auther yangZongJun
/********************************************//**
Date  : 2014/03/08
题目来源:B
题    意:stack
解题思路:
 ***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <stack>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int MAXN = 1100000;
const int INF = 2100000000;

stack<string> toFront, toBack;

int main()
{
    //freopen("input.txt", "r", stdin);
    string cmd, str, current = "http://www.acm.org/";
    while(cin >> cmd)
    {
        if(cmd == "QUIT") break;
        else if(cmd == "VISIT")
        {
            cin >> str;
            cout << str << endl;
            toBack.push(current);
            current = str;
            while(! toFront.empty())
            {
                toFront.pop();
            }
        }
        else if(cmd == "BACK")
        {
            if(! toBack.empty())
            {
                toFront.push(current);
                current = toBack.top();
                cout << current << endl;
                toBack.pop();
            }
            else cout << "Ignored" << endl;
        }
        else if(cmd == "FORWARD")
        {
            if(! toFront.empty())
            {
                toBack.push(current);
                current = toFront.top();
                cout << current << endl;
                toFront.pop();
            }
            else cout << "Ignored" << endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值