#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
class AnimalShell
{
protected:
queue<int> curShell;
queue<int> animalNumber[2];
public:
void renewShell();
void enqueue(int number, string type);
void dequeueAny(int* number, string* type);
int dequeueDog();
int dequeueCat();
};
void AnimalShell::renewShell()
{
if (!curShell.empty())
{
while (animalNumber[curShell.front()].empty())
{
if (animalNumber[curShell.front()].empty())
curShell.pop();
if (curShell.empty())
return ;
}
}
}
void AnimalShell::enqueue(int number, string type)
{
if (curShell.empty())
{
if (type == "cat")
curShell.push(0);
else
curShell.push(1);
}
else if (curShell.size() == 1)
{
if (type == "cat" && curShell.front() != 0)
curShell.push(0);
else if (type == "dog" && curShell.front() != 1)
curShell.push(1);
}
if (type == "cat")
animalNumber[0].push(number);
else
animalNumber[1].push(number);
}
void AnimalShell::dequeueAny(int* number, string* type)
{
if (curShell.empty())
{
*number = -1;
*type = "none";
}
else
{
if (curShell.front() == 0)
{
*number = dequeueCat();
*type = "cat";
}
else
{
*number = dequeueDog();
*type = "dog";
}
}
}
int AnimalShell::dequeueDog()
{
renewShell();
if (animalNumber[1].empty())
return -1;
else
{
int temp = animalNumber[1].front();
animalNumber[1].pop();
if (animalNumber[curShell.front()].empty())
curShell.pop();
return temp;
}
}
int AnimalShell::dequeueCat()
{
renewShell();
if (animalNumber[0].empty())
return -1;
else
{
int temp = animalNumber[0].front();
animalNumber[0].pop();
if (animalNumber[curShell.front()].empty())
curShell.pop();
return temp;
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
AnimalShell shell;
int n;
cin >> n;
while (n--)
{
int number = 0;
string operate,type;
cin >> operate;
if (operate == "enqueue")
{
cin >> number >> type;
shell.enqueue(number, type);
}
else
{
if (operate == "dequeueAny")
shell.dequeueAny(&number, &type);
else if (operate == "dequeueCat")
{
number = shell.dequeueCat();
if (number != -1)
type = "cat";
else
type = "none";
}
else if (operate == "dequeueDog")
{
number = shell.dequeueDog();
if (number != -1)
type = "dog";
else
type = "none";
}
cout << '[' << number << ' ' << type << "] ";
}
}
cout << endl;
}
}
Code-Animal
最新推荐文章于 2024-08-18 21:07:18 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)