SDOTOJ2088 refresh的停车场(栈和队列)

 refresh的停车场
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

 refresh近期发了一笔横財,开了一家停车场。因为土地有限。停车场内停车数量有限,可是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先
进入停车场,并且停车场的结构要求仅仅出去的车辆必须是停车场中最后进去的车辆。

现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道。

Del 表示停车场中出去了一辆车。Out 表示便道最前面的车辆不再等待。放弃进入停车场)。

如果便道内的车辆不超过1000000.

Input

 首先输入N和M(0< n。m <200000),接下来输入M条命令。

Output

 输入结束后,假设出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。

Sample Input

2 6
Add 18353364208
Add 18353365550
Add 18353365558
Add 18353365559
Del
Out

Sample Output

18353365558
18353364208

Hint

 


      非常easy的一道题。可是手残错了好几遍,实在不应该,故留下做个纪念,愿此错不再犯。。。


模拟栈和队列做法:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<stack>
#include<queue>

using namespace std;

string a[2000021],b[2000021];
int n,m;

int main()
{
    while(cin >> n >> m)
    {
        int flag = 0;
        string str,num;
        int h = 0,k1 = 0,k2 = 0;
        for(int i=0; i<m; i++)
        {
            cin >> str;
            if(str == "Add")
            {
                cin >> num;
                if(h<n)
                {
                    a[++h] = num;
                }
                else
                {
                    b[++k2] = num;
                }

            }
            else if(str == "Del")
            {
                if(h<=0)
                {
                    flag = 1;
                }
                else if(k2 > k1)
                {

                    a[h] = b[++k1];
                }
                else if(k2<=k1)
                {
                    h--;
                }

            }
            else if(str == "Out")
            {
                if(k2 <= k1)
                {
                    flag = 1;
                }
                else
                {
                    k1++;
                }

            }

        }
        if(h>0 && flag == 0)
        {
            while(h>0)
            {
                cout << a[h] << endl;
                h--;
            }
        }
        else if(flag == 1)
        {
            cout << "Error" << endl;
        }

    }
    return 0;
}




STL做法:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include<stack>
#include<queue>

using namespace std;

int n,m;

int main()
{
    while(cin >> n >> m)
    {
        stack<string>a;
        queue<string>b;
        int flag = 0;
        string str,num;
        for(int i=0; i<m; i++)
        {
            cin >> str;
            if(str == "Add")
            {
                cin >> num;
                if(a.size() == n)
                {
                    b.push(num);
                }
                else
                {
                    a.push(num);
                }
            }
            else if(str == "Del")
            {

                if(a.empty())
                {
                    flag = 1;
                }
                else
                {
                    a.pop();
                    if(!b.empty())
                    {
                        a.push(b.front());
                        b.pop();
                    }
                }
            }
            else if(str == "Out")
            {

                if(b.empty())
                {
                    flag = 1;
                }
                else
                {
                    b.pop();
                }
            }
            // printf("h = %d    k1 = %d   k2 = %d
",h,k1,k2);
        }

        if(flag == 1)
        {
            cout << "Error" << endl;
        }
        else
        {
            while(!a.empty())
            {
                cout << a.top() << endl;
                a.pop();
            }

        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/bhlsheji/p/5255185.html