2 6 Add 18353364208 Add 18353365550 Add 18353365558 Add 18353365559 Del Out
18353365558 18353364208
算法分析:使用STL的栈和队列 来做的,比较好描述。
用栈来模拟停车场,后进先出。 用队列来模拟候车的便道,先来先进停车场。
注意:即使 中间的输入处理命令不合理,需要输出Error 那也得把所有的命令输入完毕之后在输出Error,也就是说
一旦出现不合理的情况,则需要标记。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int n, m;
char s[50]; string t;
int main()
{
int dd, i;
int flag;
while(scanf("%d %d", &n, &m)!=EOF)
{
stack<string>q;
queue<string>w;
flag=1;
for(i=0; i<m; i++)
{
cin>>s;
if(s[0]==‘A‘ )
{
cin>>t;
if(q.size()==n)
{
w.push(t); //进入队列
}
else
{
q.push(t);
}
}
else if(s[0]==‘D‘)
{
if(q.empty())
{
flag=0;
}
else
{
q.pop();
if(!w.empty())
{
t=w.front();
q.push(t);
w.pop();
}
}
}
else
{
if(w.empty())
{
flag=0;
}
else
{
w.pop();
}
}
}
if(flag==0)
printf("Error\n");
else
{
while(!q.empty())
{
t=q.top();
cout<<t<<endl;
q.pop();
}
}
}
return 0;
}
原文:http://www.cnblogs.com/yspworld/p/4049145.html