以前在uva上做过这个题,一模一样的,但是这次还是弄了个RE,而且这次的错误与上次完全不同;
以前的错误是逻辑错误,这次的而代码错误,只是少个else。
用了一晚上的时间,用神一样的测试数据终于揪出了这个错误。
错误见代码《
测试数据见UVA540.
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
struct node
{
int data;
node *next;
};
struct zu
{
int count;
node *tail, *head;
};
int cla[1000050];
zu z[1050];
node* head = NULL, *tail = NULL;
void input(int t)
{
int n, temp;
for(int i = 1; i <= t; i++)
{
cin>>n;
while(n--)
{
cin>>temp;
cla[temp] = i;
}
}
}
void enqueue()
{
int temp;
cin>>temp;
node *q = (node*)malloc(sizeof(node));
q->data = temp;
q->next = NULL;
if(!head)
{
head = tail = q;
z[cla[temp]].count = 1;
z[cla[temp]].head = z[cla[temp]].tail = q;
}
else if(!z[cla[temp]].count)//千呼万唤始出来的错误,此处少了个else,也算的上是逻辑漏洞,以为我的愿意是正确的,-_-#!
{
z[cla[temp]].count = 1;
z[cla[temp]].head = z[cla[temp]].tail = q;
tail->next = q;
tail = q;
}
else
{
z[cla[temp]].count++;
q->next = z[cla[temp]].tail->next;
z[cla[temp]].tail->next = q;
z[cla[temp]].tail = q;
if(tail->next == q)tail = q;
}
}
void dequeue()
{
node *q = head;
q = head;
z[cla[q->data]].count--;
z[cla[q->data]].head = z[cla[q->data]].head->next;
cout<<q->data<<endl;
head = head->next;
delete q;
}
int main ()
{
int t, num = 0;
string s;
while(cin>>t&&t)
{
head = tail = NULL;
memset(z,0,sizeof(z));
input(t);
cout<<"Scenario #"<<++num<<endl;
while(cin>>s)
{
cin.ignore();
if(s == "ENQUEUE")enqueue();
if(s == "DEQUEUE")dequeue();
if(s == "STOP")
break;
}
cout<<endl;
}
return 0;
}