Cod sursa(job #803837)

Utilizator tibi2012Galatanu Tiberiu tibi2012 Data 28 octombrie 2012 13:38:43
Problema Perle Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1 kb
var v:array[1..10000] of byte;
    n,i,j,l:longint;
    f,g:text;
function c(poz:word):word;
  forward;

function b(poz:word):word;
begin
  if l-poz+1<5 then
    b:=0
  else
    if v[poz]=2 then
      b:=b(poz+1)
    else
      if (v[poz]=1) and (v[poz+2]=3) then
        b:=c(poz+4);
end;

function c(poz:word):word;
var loc:word;
begin
  if v[poz]=2 then
    c:=poz+1
  else
    if (v[poz]=3) and (l-poz>=6) then
      begin
        loc:=b(poz+1);
        if loc=0 then
          c:=0
        else
          c:=c(loc);
      end
    else
      if (l-poz>=2) and (v[poz]=1) and (v[poz+1]=2) then
        c:=poz+3
      else
        c:=0;
end;

begin
  assign(f,'perle.in');
  assign(g,'perle.out');
  reset(f);
  rewrite(g);
  readln(f,n);
  for i:=1 to n do
    begin
      read(f,l);
      for j:=1 to l do
        read(f,v[j]);
      if (l=1) or (b(1)=l+1) or (c(1)=l+1) then
        writeln(g,1)
      else
        writeln(g,0);
    end;
  close(f);
  close(g);
end.