Cod sursa(job #223002)

Utilizator florin.moldovanuFlorin M florin.moldovanu Data 26 noiembrie 2008 17:07:07
Problema Perle Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.42 kb
#include<stdio.h>

int y[10002], l,n,vf;
char st[10002];
FILE *f,*g;

void citire()
{
int i;
fscanf(f,"%d",&l);
vf=0;
for(i=1;i<=l;i++)
 fscanf(f,"%d",&y[i]);
fclose(f);
}

int verifica()
{     int i,j;
if(l==1) return 1;
if(l==2) return 0;
if(y[1]==2) {st[1]='2';st[2]='B';vf=2;}
if(y[1]==3) {st[1]='3';st[2]='B';st[3]='C';vf=3;}
if(y[1]==1)
  if(y[3]==3){st[1]='1';st[2]='A';st[3]='3';st[4]='A';st[5]='C';vf=5;}
  else if(y[2]==2) {st[1]='1';st[2]='2';st[3]='A';vf=3;}
       else return 0;
for(i=2;i<=vf&& i<=n;i++)
 if(st[i]=='B')
   { if(y[i]==2) {for(j=vf;j>=i+1;j--) st[j+1]=st[j];
		 st[i]='2';st[i+1]='B';vf++;}
      else if(y[i]==1) {for(j=vf;j>=i+1;j--) st[j+4]=st[j];
		 st[i]='1';st[i+1]='A';st[i+2]='3';st[i+3]='A';st[i+4]='C';vf+=4;}
	   else return 0;
   }
  else
  if(st[i]=='C')
    {if(y[i]==3){for(j=vf;j>=i+1;j--) st[j+2]=st[j];
		 st[i]='3';st[i+1]='B';st[i+2]='C';vf+=2;}
      else if(y[i]==1) {for(j=vf;j>=i+1;j--) st[j+2]=st[j];
		 st[i]='1';st[i+1]='2';st[i+2]='A';vf+=2;}
	   else if(y[1]==2) st[i]='2';
		else return 0;
    }
  else
  if(st[i]=='A')
     st[i]=y[i]+'0';
  else
  if(st[i]-'0'!=y[i]) return 0;

if(i<=vf ||i<=n) return 0;

return 1;
}




int main()
{
int i;
f=fopen("perle.in","r");
f=fopen("perle.out","w");
fscanf(f,"%d",&n);
for(i=1;i<=n;i++)
{citire();
if(verifica()) fprintf(g,"1\n");
else fprintf(g,"0\n");
}
  return 0;

}