Cod sursa(job #137439)

Utilizator DiaconuDiaconu Loredana Diaconu Data 17 februarie 2008 12:12:09
Problema Lampa Scor 10
Compilator fpc Status done
Runda preONI 2008, Runda 4, Clasa a 9-a Marime 1.7 kb
var    f,g:text;
       ok,ok1,ok2,da:boolean;
       x,y,z:string;
       i,n,m,nr,num,j,l,k,k1,s,a,b:integer;
       c:array[1..100000] of char;

begin
assign (f,'lampa.in');reset (f);
assign (g,'lampa.out');rewrite (g);
readln (f,n,m);
x:='a';
y:='b';
for i:=3 to n do begin
 z:=x+y;
 x:=y;
 y:=z;
end;
for i:=1 to length (z) do
 if z[i]='a' then inc(a)
             else inc(b);
for i:=1 to m do
 read (f,c[i]);
ok:=true;
i:=0;
da:=false;
while (ok) and (i<=m) do begin
 inc (i);
 x:='';
 nr:=m-(i*a);
 if (nr mod b)=0 then begin
  num:=nr div b;
  nr:=i;
  if z[1]='a' then for j:=1 to nr do x:=x+c[j]
              else begin
               ok1:=true;
               s:=0;
               for k:=1 to length(z) do begin
                if (z[k]='a') and (ok1) then begin
                 for k1:=1 to nr do
                  x:=x+c[k1+s];
                 ok1:=false;
                 end;
                if z[k]='a' then s:=s+nr
                            else s:=s+num;
               end;
               end;
  da:=true;
  s:=0;
  for j:=1 to length(z) do begin
   if z[j]='a' then begin
    for l:=1 to nr do
     if x[l]<>c[s+l] then da:=false;
   end;
   if z[j]='a' then s:=s+nr
               else s:=s+num;
  end;
 end;
 if da then ok:=false;
end;
ok1:=true;
ok2:=true;
s:=0;
y:='';
for i:=1 to length(z) do begin
 if (z[i]='a') and (ok1) then begin
  for j:=1 to nr do
   write (g,c[j+s]);
  writeln(g);
  ok1:=false;
 end
 else if (z[i]='b') and (ok2) then begin
  for j:=1 to num do
   y:=y+c[j+s];
  ok2:=false;
 end;
 if z[i]='a' then s:=s+nr
             else s:=s+num;
end;
writeln (g,y);
close(f);
close(g);
end.