rt.
思路就是用 map 记录一个元素是否存在,当队首元素不存在时弹出并跳过。
其他的就是纯模拟,但是看不出哪错了
#include <iostream>
#include <string>
#include <queue>
#include <map>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
queue<string> q;
map<string,bool> m;
string str,op,playing[2];
int n,playing_len=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>op;
if(op=="start")
{
for(int j=0;j<playing_len;j++)
{
q.push(playing[j]);
}
playing_len=0;
while(playing_len<2 and !q.empty())
{
if(m.find(q.front())==m.end())// element doesn't exist
{
q.pop();
continue;
}
playing[playing_len]=q.front();
cout<<q.front()<<' ';
playing_len++;
q.pop();
}
if(playing_len==0)
{
cout<<"Error\n";
}
else
{
cout<<endl;
}
}
else if(op=="arrive")
{
cin>>str;
if(m.find(str)!=m.end())
{
cout<<"Error\n";
}
else
{
m[str]=true;
q.push(str);
cout<<"OK\n";
}
}
else// leave
{
cin>>str;
bool flag=false;
for(int i=0;i<playing_len;i++)
{
if(playing[i]==str)
{
flag=true;
break;
}
}
if(m.find(str)==m.end() or flag)
{
cout<<"Error\n";
}
else
{
m.erase(m.find(str));
cout<<"OK\n";
}
}
}
}