398 - 18-Wheeler Caravans (aka Semigroups)

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
dabendan
New poster
Posts: 13
Joined: Mon May 06, 2002 4:05 pm
Location: ROC

398 - 18-Wheeler Caravans (aka Semigroups)

Post by dabendan »

Is there any trick in problem 398 ?
I can't find any problem.
My output is totally the same with sample output.
[c]#include <stdio.h>
#include <stdlib.h>

char a[30][30];
char node[30];
int n;
int started=0;

int main()
{
int index;
void Input();
void Display();
void Proceed();
while(1)
{
Input();
Display();
Proceed();
for(index=0;index<30;index++)
printf("-");
}
}

void Proceed()
{
int index,index2,index3,flag;
int Search(char);

for(index=0;index<n;index++)
{
for(index2=0;index2<n;index2++)
{
flag=Search(a[index][index2]);
if(flag==-1)
{
printf("NOT A SEMIGROUP: %c#%c = %c WHICH IS NOT AN ELEMENT OF THE SET\n",node[index],node[index2],a[index][index2]);
return;
}
}
}

for(index=0;index<n;index++)
{
for(index2=0;index2<n;index2++)
{
flag=0;
for(index3=0;index3<n;index3++)
{
if(a[Search(a[index][index2])][index3]!=a[index][Search(a[index2][index3])])
{
flag=1;
break;
}
}
if(flag==1)
{
printf("NOT A SEMIGROUP: (%c#%c)#%c IS NOT EQUAL TO %c#(%c#%c)\n",node[index],node[index2],node[index3],node[index],node[index2],node[index3]);
return;
}
}
}

for(index=0;index<n;index++)
for(index2=0;index2<n;index2++)
{
if(a[index][index2]!=a[index2][index])
{
printf("SEMIGROUP BUT NOT COMMUTATIVE (%c#%c IS NOT EQUAL TO %c#%c)\n",node[index],node[index2],node[index2],node[index]);
return;
}
}
printf("COMMUTATIVE SEMIGROUP\n");
return;
}

int Search(char target)
{
int index,flag=0;
for(index=0;index<n;index++)
{
if(node[index]==target)
{
flag=1;
break;
}
}
if(flag==1)
return(index);
else
return(-1);
}

void Display()
{
int index,index2;
printf("S = {");
for(index=0;index<n;index++)
{
printf("%c",node[index]);
if(index!=n-1)
printf("%c",',');
}
printf("}\n");
printf(" #|");
for(index=0;index<n;index++)
printf("%c",node[index]);
printf("\n -+");
for(index=0;index<n;index++)
printf("-");
printf("\n");
for(index=0;index<n;index++)
{
printf(" %c|",node[index]);
for(index2=0;index2<n;index2++)
printf("%c",a[index][index2]);
printf("\n");
}
printf("\n");
}

void Input()
{
int index,index2;
char temp[30];
fscanf(stdin,"%d",&n);
if(n==0)
exit(0);
else
if(started!=0)printf("\n\n");
else
started=1;
fscanf(stdin,"%s",temp);
for(index=0;index<n;index++)
node[index]=temp[index];
for(index=0;index<n;index++)
{
fscanf(stdin,"%s",temp);
for(index2=0;index2<n;index2++)
{
a[index][index2]=temp[index2];
}
}
}[/c]

Balon
New poster
Posts: 8
Joined: Tue Nov 26, 2002 6:00 am

Post by Balon »

you should print two end-of-line ('\n') after each test case ( include the last case).
if you print only one '\n' after the last case, you'll got a P.E.
if you print no '\n' after the last case , I think for any problem , you can only got WA :wink:
sample code like this:
[cpp]

for( ; ; )
{
// get input
if(n == 0)
return;
proccess();
printf("------------------------------");
printf("\n\n");
}


[/cpp]

dabendan
New poster
Posts: 13
Joined: Mon May 06, 2002 4:05 pm
Location: ROC

Post by dabendan »

Thanks a lot. :D
It's my mistake.
I should review my code more carefully.

Post Reply

Return to “Volume 3 (300-399)”