Cod sursa(job #253919)

Utilizator Teodor94Teodor Plop Teodor94 Data 6 februarie 2009 13:37:40
Problema Episoade Scor 0
Compilator fpc Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 1 Marime 2.83 kb
var f,g:text;
    s,s2,s1:string;
    x,i,t,n,j,k,x1,x2:longint;
    err:integer;
    a:array[0..20] of byte;
    p,p1,x1x2:boolean;
begin
  assign(f,'episoade.in'); reset(f);
  assign(g,'episoade.out'); rewrite(g);
  readln(f,s);
  x:=0;
  x:=pos('(',s);
  read(f,t,n);
  for i:=1 to t do
    begin
      for j:=1 to n do
        read(f,a[j]);
      if x=0 then
        begin
          for j:=1 to length(s) do
            if s[j]='>' then
              begin
                if (s[j-2]<>'>') and (s[j-2]<>'#') then
                  s1:=''+s[j-2]+s[j-1]
                                                   else
                  s1:=s[j-1];
                if (s[j+2]<>'>') and (s[j+2]<>'#') then
                  s2:=''+s[j+1]+s[j+2]
                                                   else
                  s2:=s[j+1];
                val(s1,x1,err);
                val(s2,x2,err);
                p:=true;
                for k:=1 to n do
                  if (a[k]=x1) and (a[k+1]<>x2) then begin p:=false; break; end
                                                else
                  if (a[k]=x1) and (a[k+1]=x2) then break;
                if not p then break;
              end;
          if not p then writeln(g,'0')
                   else
          begin
            for j:=1 to length(s) do
              if s[j]='#' then
                begin
                  p1:=true;
                  if (s[j-2]<>'>') and (s[j-2]<>'#') then
                    s1:=''+s[j-2]+s[j-1]
                                                     else
                    s1:=s[j-1];
                  if (s[j+2]<>'>') and (s[j+2]<>'#') then
                    s2:=''+s[j+1]+s[j+2]
                                                     else
                    s2:=s[j+1];
                  val(s1,x1,err);
                  val(s2,x2,err);
                  x1x2:=false;
                  if (s[j-2]='>') or (s[j-3]='>') or (s[j+2]='>')
                  or (s[j+3]='>') then x1x2:=true;
                  if x1x2 then
                    for k:=1 to n do
                      if (a[k]=x2) and (a[k+1]=x1) then begin p1:=false; break; end
                                                   else
                      if (a[k]=x1) and (a[k+1]=x2) then break;
                  if not x1x2 then
                    for k:=1 to n do
                      if ((a[k]=x1) and (a[k+1]<>x2))
                      or ((a[k]=x2) and (a[k+1]<>x1)) then begin p1:=false; break; end
                                                      else
                      if ((a[k]=x1) and (a[k+1]=x2))
                      or ((a[k]=x2) and (a[k+1]=x1)) then break;
                  if p1 then writeln(g,'1')
                        else writeln(g,'0');
                end;
          end;
        end;
    end;
  close(f); close(g);
end.