Parking lot for SDOTOJ2088 refresh
Parking lot for refreshTime Limit:1000 MSMemory Limit:65536KB64bit IO Format:% Lld & % lluSubmit Status
Description
Refresh recently made a fortune and opened a parking lot. Due to limited land, the number of parking in the parking lot is limited, but there are too many vehicles required to enter the parking lot. When the parking lot is full, the vehicles to enter will enter the convenient road and wait. The first vehicles to enter the convenient road will first enter the parking lot, in addition, the structure of the parking lot requires that only the vehicles going out must be the last vehicles in the parking lot. Now we will tell you the parking lot capacity N and the command M, and some commands (Add num indicates that a vehicle with the license plate number num is going to enter the parking lot or driveway. Del indicates that a car is going out in the parking lot, out indicates that the vehicle at the top of the sidewalk is no longer waiting and stops entering the parking lot ). Assume that the number of vehicles in the driveway cannot exceed 1000000.
Input
Enter N and M (0 <n, m <200000), and then enter M commands.
Output
After the input is complete, if there is no vehicle in the parking lot and Del or there is no vehicle in the driveway and Out, an Error is output; otherwise, the vehicle in the parking lot is output first, no vehicle, no output.
Sample Input
2 6Add 18353364208Add 18353365550Add 18353365558Add 18353365559DelOut
Sample Output
1835336555818353364208
Hint
This is a very simple question, but the hand has been wrong several times. It is really not necessary, so I will leave it as a souvenir. May I not make this mistake again...
Stack and queue simulation practices:
#include
#include#include
#include
#include
#include
#include
#include
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
> str; if(str == "Add") { cin >> num; if(h
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 practices:
#include
#include#include
#include
#include
#include
#include
#include
using namespace std;int n,m;int main(){ while(cin >> n >> m) { stack
a; queue
b; int flag = 0; string str,num; for(int i=0; i
> 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\n",h,k1,k2); } if(flag == 1) { cout << "Error" << endl; } else { while(!a.empty()) { cout << a.top() << endl; a.pop(); } } } return 0;}