Here is my judge for BFK_AUTO3:
/*
Author: Tjandra Satria Gunawan
Judge for: http://www.spoj.com/problems/AUTO_BFK/
Created: 24 May 2013
Finished: 26 May 2013
Changes:
-(03 June 2013)Fixed: too many allocated memory
Thanks to Mitch Schwartz for repporting this bug
*/
#include"spoj.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef unsigned u;
typedef struct S//struct that contatin BF cell, using linked list so total cell available is unlimited! (only limited by SPOJ memory limit)
{
u data;//*ptr value (initial value is all 0)
struct S *next,*prev;//next and previous *ptr value
}v;
typedef struct T//struct that contain BF code, using linked list so total code that can be executed is unlimited! (only limited by SPOJ memory limit)
{
char cmd;//valid command is only: {'+','-','<','>','[',']','.'}
u rpt;//for optimization only, if there are some repeated command this value will increase
struct T *next,*jump;//"*next" is next character and "*jump" is useful when '[' and ']' command is executed
}w;
typedef struct M//struct that contain memory address to "struct T" (temporary only: added when '[' command found and stored until closing ']' command found)
{
w *loc;//this value will copied to "*jump" in "struct T" after ']' command found
struct M *prev;//this is linked to previous temporary value
}m;
u invalid[128];//value for valid BF ASCII (except comma ',') is 0 otherwise is 1
#define csn 1//case number, starting from 1
#define start 1//1 for judging first test data, otherwise it set to 0
#define end 0//1 for judging last test data, otherwise it set to 0
//main code begin!
int main()
{
spoj_init();
double st=clock();
if(start)
{
fprintf(spoj_u_info,"<u>Last Judge Update</u>: 03 June 2013 (Indonesian time)<br><u>Recent Changes</u>: More efficient memory management but the judge running time will be slower<br><table class=\"judge_output\" width=\"100%c\"><tbody><tr class=\"headerrow\"><th width=\"10%c\" class=\"headerrowleft\">Case Number</th><th width=\"10%c\">I/O Length</th><th width=\"10%c\">BF Length</th><th width=\"10%c\">BF Cell Used</th><th width=\"10%c\">BF Code Called</th><th width=\"10%c\">Judge Time</th><th width=\"40%c\" class=\"headerrowright\">The Judge's Comment</th></tr>",37,37,37,37,37,37,37,37);
}
u len,clu,outl,inl,exec,rexec,exbuf,i;
/*
len=BF code length
clu=BF Cell Used
outl=Output length
inl=Input length
exec=Executed command
rexec=Real executed command
exbuf=just to check if the judge judge is TLE or not
i=additional variable (for iteration)
*/
for(i=0;i<128;invalid[i++]=1);
len=outl=rexec=exec=0;
fseek(spoj_p_in,0,SEEK_END);
inl=ftell(spoj_p_in);clu=1;exbuf=1000000;
fseek(spoj_p_in,0,SEEK_SET);
invalid['+']=invalid['-']=invalid['<']=invalid['>']=invalid['[']=invalid[']']=invalid['.']=0;
m *now,*new;now=NULL;
v *ptr;
w *prog,*tmp;
ptr=(v*)malloc(sizeof(v));
ptr->data=0;ptr->next=ptr->prev=NULL;
char c;
prog=tmp=(w*)malloc(sizeof(w));tmp->jump=tmp->next=NULL;
if(prog==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
while((c=fgetc(spoj_t_out))>0)
{
if(!invalid[c])break;
if(c==',')//of course comma: ','(ASCII(44)) is valid BF code but it's invalid here, so I don't accept this output :-p
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>There's a comma: ','(ASCII(44)) character on your BF code which is forbidden!</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
}
if(c>0)
{
tmp->cmd=c;
tmp->rpt=1;
if(c==']')
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>invalid BF code: '['(ASCII(91)) and ']'(ASCII(93)) is not match! (too many ']'(ASCII(93)))</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
if(c=='[')
{
now=(m*)malloc(sizeof(m));
if(now==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
now->prev=NULL;
now->loc=tmp;
}
len++;
}
while((c=fgetc(spoj_t_out))>0)
{
if(invalid[c])
{
if(c==',')
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>There's a comma: ','(ASCII(44)) character on your BF code which is forbidden!</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
continue;
}
len++;
if(c!='['&&c!=']')
{
if(c==tmp->cmd){tmp->rpt++;continue;}
if((tmp->cmd=='+'&&c=='-')||(tmp->cmd=='-'&&c=='+')||(tmp->cmd=='>'&&c=='<')||(tmp->cmd=='<'&&c=='>'))
{
if(tmp->rpt)tmp->rpt--;
else
{
tmp->rpt=1;
if(tmp->cmd=='+')tmp->cmd='-';
else if(tmp->cmd=='-')tmp->cmd='+';
else if(tmp->cmd=='>')tmp->cmd='<';
else tmp->cmd='>';
}
continue;
}
}
if(tmp->rpt)
{
tmp->next=(w*)malloc(sizeof(w));
tmp=tmp->next;
if(tmp==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
tmp->jump=tmp->next=NULL;
}
tmp->cmd=c;
tmp->rpt=1;
if(c==']')
{
if(now==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>invalid BF code: '['(ASCII(91)) and ']'(ASCII(93)) is not match! (too many ']'(ASCII(93)))</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
else
{
new=now;now=now->prev;
tmp->jump=new->loc;
tmp->jump->jump=tmp;
free(new);
}
}
else if(c=='[')
{
if(now==NULL)
{
now=(m*)malloc(sizeof(m));
if(now==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
now->prev=NULL;
}
else
{
new=(m*)malloc(sizeof(m));
if(new==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
new->prev=now;now=new;
}
now->loc=tmp;
}
}
if(now!=NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>invalid BF code: '['(ASCII(91)) and ']'(ASCII(93)) is not match! (too many '['(ASCII(91)))</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
for(tmp=prog;tmp!=NULL;tmp=tmp->next)
{
rexec++;
if((exec+=tmp->rpt)>exbuf)
{
exbuf+=1000000;//time to check the time, TLE or not?
if((clock()-st)/CLOCKS_PER_SEC>22)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Running time >22s (Judge's TLE!) please simplify your BF code!</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
}
if(tmp->cmd=='+')ptr->data+=tmp->rpt;
if(tmp->cmd=='-')ptr->data-=tmp->rpt;
if(tmp->cmd=='[')if(!ptr->data)tmp=tmp->jump;
if(tmp->cmd==']')if(ptr->data)tmp=tmp->jump;
if(tmp->cmd=='>')for(i=tmp->rpt;i--;)
{
if(ptr->next!=NULL)ptr=ptr->next;
else
{
ptr->next=(v*)malloc(sizeof(v));
if(ptr->next==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
ptr->next->prev=ptr;ptr=ptr->next;
ptr->data=0;ptr->next=NULL;clu++;
}
}
if(tmp->cmd=='<')for(i=tmp->rpt;i--;)
{
if(ptr->prev!=NULL)ptr=ptr->prev;
else
{
ptr->prev=(v*)malloc(sizeof(v));
if(ptr->prev==NULL)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Can't allocate more cells! (SPOJ Memory Problem)</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
ptr->prev->next=ptr;ptr=ptr->prev;
ptr->data=0;ptr->prev=NULL;clu++;
}
}
if(tmp->cmd=='.')for(i=tmp->rpt;i--;)
{
outl++;
c=fgetc(spoj_p_in);
if(c<0)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Output of your BF code is too many! Your program should exit, but you still printing '%c'(ASCII(%u)) character!</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC,ptr->data,ptr->data);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
if(c!=ptr->data)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Output of your BF code is not match with the test data: it should print '%c'(ASCII(%u)) but your BF code is printing '%c'(ASCII(%u))</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC,c,c,ptr->data,ptr->data);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
}
}
if((c=fgetc(spoj_p_in))>0)
{
fprintf(spoj_u_info,"<tr class=\"kol\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Your BF code exit before completely print entire correct output data: Next character to print is '%c'(ASCII(%u))</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC,c,c);
if(end)fprintf(spoj_u_info,"</tbody></table>");return 1;
}
fprintf(spoj_u_info,"<tr class=\"kol3\" align=\"center\"><td>%u</td><td>%u<br>%u</td><td>%u</td><td>%u</td><td>%u<br>%u</td><td>%.2lf</td><td>Congratulations, Your code run correctly without any problem for this case</td></tr>",csn,inl,outl,len,clu,exec,rexec,(clock()-st)/CLOCKS_PER_SEC);
if(end)fprintf(spoj_u_info,"</tbody></table>");
fprintf(spoj_score,"%u\n",len);
return 0;
}