想想在大神看来或许一道简单得不行的水题足足让我重编了3个程序,提交了n次,调了整整两天,菜鸟本质暴露无遗呀,唉~算了是人都会有这一步的,不过神就不一样了哈,不过在此忽略大神的存在给自己一丝安慰吧
现在想来这个题的最主要问题就是没把题目读明白,人家要求一次DNQUEUE输出一次删除的节点,结果嘞,错了吧,再来删除的顺序都没能很确定,是按插入顺序还是队列中的先后。。。还有好多问题根本就没想清楚就动手,结果就杯了个大具。。。。以后一定要弄明白题目要求再动手,虽然晚动手,但绝对是事半功倍的,谨记教训!
言归正传,这是题目要求:
Queues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.
In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.
Your task is to write a program that simulates such a team queue.
Input
The input will contain one or more test cases. Each test case begins with the number of teams t (1<=t<=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.
Finally, a list of commands follows. There are three different kinds of commands:
- ENQUEUE x - enter element x into the team queue
- DEQUEUE - process the first elementand remove it from the queue
- STOP - end of test case
The input will be terminated by a value of 0 for t.
Output
For each test case, first print a line saying "Scenario #k", where k is the number of the test case. Then, for eachDEQUEUE command, print the element which is dequeued on a single line. Print a blank lineafter each test case, even after the last one.
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#define MAX 1000
#define MAXE 1000000
using namespace std;
struct node
{
int num;
node *next;
};
typedef struct Team
{
node *front;//team中的原始(头)成员
node *rear;//指向末尾成员
}team[MAX];
int elementNum[MAXE] = {0};//记录进队成员号所对应的team号
int scenario = 0;//表示是第几组测试用例,用于之后输出
int order[MAX];
int orderNum = 0;
int dnum = 1;
int k = 0;//有几个队
bool tag[MAX];
void MakeNull(Team team[],int a)//对team[a]完成初始化
{
team[a].front = new node;
team[a].front->next = NULL;
team[a].front->num = 0;
team[a].rear = team[a].front;
}
void ENqueue(Team team[])
{
int a;//记录以读入数为下标所对应的team值
int e;
node *cell = NULL;
cin>>e;
a = elementNum[e];
if(a == 0){
elementNum[a] = k++;//之前没有一个队中的则进入对尾成为一个新队
MakeNull(team,k);
}
else
{
cell = new node;
cell->num = e;
cell->next = NULL;
team[a].rear->next = cell;
team[a].rear = cell;
if(!tag[a]){
order[++orderNum] = a;
tag[a] = true;
}
}
}
void DNqueue(Team team[])
{
int a;
node *cell = NULL;
a = order[dnum];
if(a == 0){
cout<<0<<endl;
return;
}
while(team[a].front->next == NULL){
team[a].rear = team[a].front;
tag[a] = false;
a = order[++dnum];
if(a == 0){
cout<<0<<endl;
return;
}
}
cell = team[a].front->next;
team[a].front->next = cell->next;
if(cell->next == NULL){
tag[a] = false;
team[a].rear = team[a].front;
dnum++;
}
cout<<cell->num<<endl;
delete cell;
}
int main()
{
string commend;
Team team[MAX];
int j = 0;//team中有几个队员
int e;
cin>>k;
while(k > 0){
memset(tag,false,sizeof(tag));
memset(order,0,sizeof(order));
memset(elementNum,0,sizeof(elementNum));
dnum = 1;
orderNum = 0;
scenario++;
cout<<"Scenario #"<<scenario<<endl;
for(int a = 1;a <= k;a++){//读入每个队中的数
MakeNull(team,a);//将即将读入数据的team初始化
cin>>j;//每个team中的队员数
for(int b = 0;b < j;b++){
cin>>e;
elementNum[e] = a;//以该队员号为下表的数组
}
}
cin>>commend;
while("STOP" != commend){//读入命令,分别执行
if(commend == "ENQUEUE")
ENqueue(team);
else
DNqueue(team);
cin>>commend;
}
cout<<endl;
cin>>k;
}
return 0;
}