用map做的,样例过,求调/hack
  • 板块P9518 queue
  • 楼主Jianbing_Juan
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/10/5 02:14
  • 上次更新2023/11/2 15:37:59
查看原帖
用map做的,样例过,求调/hack
940854
Jianbing_Juan楼主2023/10/5 02:14

rt.

record

思路就是用 map 记录一个元素是否存在,当队首元素不存在时弹出并跳过。

其他的就是纯模拟,但是看不出哪错了

code


#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";
			}
			
		}
	}
}

2023/10/5 02:14
加载中...