Cod sursa(job #277281)

Utilizator cheery_g1rlHaller Emanuela cheery_g1rl Data 11 martie 2009 16:57:44
Problema Perle Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.99 kb
var v,vv:array[1..10001] of char;
    t,l,ll,k,i,j:longint;
    ok:boolean;
procedure ins2b;
   begin
     for k:=ll+1 downto j+2 do vv[k]:=vv[k-1];
     inc(ll);
     vv[j]:='2'; vv[j+1]:='b';
   end;
procedure ins1a3ac;
   begin
     for k:=ll+4 downto j+5 do vv[k]:=vv[k-4];
     inc(ll,4);
     vv[j]:='1'; vv[j+1]:=v[j+1]; vv[j+2]:='3'; vv[j+3]:=v[j+3]; vv[j+4]:='c';
   end;
procedure ins3bc;
  begin
    for k:=ll+2 downto j+3 do vv[k]:=vv[k-2];
    inc(ll,2);
    vv[j]:='3'; vv[j+1]:='b'; vv[j+2]:='c';
  end;
procedure ins12a;
  begin
    for k:=ll+2 downto j+3 do vv[k]:=vv[k-2];
    inc(ll,2);
    vv[j]:='1'; vv[j+1]:='2'; vv[j+2]:=v[j+2];
  end;
begin
assign(input,'perle.in'); reset(input);
assign(output,'perle.out'); rewrite(output);
readln(t);
for i:=1 to t do
 begin
   read(l);
   for j:=1 to l do
     begin
       read(k);
       if k=1 then v[j]:='1'
              else if k=2 then v[j]:='2'
                          else if k=3 then v[j]:='3';
     end;
   if l=1 then vv[1]:='a'
     else if v[1]='3' then vv[1]:='c'
         else if (v[1]='1')and(v[2]='2')and(l=3) then vv[1]:='c'
             else vv[1]:='b';
   ll:=1;
   j:=1;
   while (j<=ll) do
     begin
       if vv[j]='a' then vv[j]:=v[j]
         else if vv[j]='b' then
                 begin
                   if v[j]='1' then ins1a3ac
                      else if v[j]='2' then ins2b;
                 end
                         else
                      if vv[j]='c' then
                         begin
                           if v[j]='1' then ins12a
                              else if v[j]='2' then vv[j]:='2'
                                  else  if v[j]='3' then ins3bc;
                         end;
       inc(j);
     end;
    ok:=true;
    if l<>ll then  ok:=false
      else
        begin
          for j:=1 to ll do if v[j]<>vv[j] then ok:=false;
        end;
    if ok then writeln('1')
      else writeln('0');
 end;
close(output);
close(input);
end.