{ Ši programa pilnai teksto neiškoduoja. Ji atkoduoja tiek, kad likusias
  raides galima nuspėti }
program kodavimas;

  const UZK = 'uzk.dat';
        FR = 'fr.dat';
        RZ = 'atko.rez';
        MAX = 20*80;
        MAXS = 100; { maksimalus skirtingu rasto zenklu skaicius }

  type masyvas = array [1..MAX] of char;  { simboliu masyvas }
       lg_masyvas = array [1..MAXS] of boolean;
       raktas = array [char] of char;

  { zemiau aprasyti globalieji kintamieji }
  var F, U: integer;
      uz, fra: masyvas;
      rakt, rakf: masyvas; { raktu masyvas }

  procedure spausdinti;
    var fl: text;
         i: integer;
  begin
    assign (fl, RZ);
    rewrite (fl);
    for i := 1 to U do
      write (fl, uz[i]);
    close (fl);
  end; { spausdinti }

  procedure atkoduoti (nr: integer);
     var c: char;
         i: integer;
         rakt: raktas;
  begin
    { raktas nerastas }
    fillchar (rakt, sizeof(rakt), '+');
    { randame rakta }
    for i := 1 to F do
      begin
        c := uz[nr+i-1];
        rakt[c] := fra[i];
      end;
    { atkoduojame }
    for i := 1 to U do
      uz[i] := rakt[uz[i]];
  end; { atkoduoti }

  procedure tapatinti;
    var i, j, inu, inf: integer;
        tinka: boolean;
        buvo: lg_masyvas;
        ct, cf: char;
  begin
    for i := 1 to U-F+1 do
      begin
        tinka := true;
        for j := 1 to F do
           buvo[j] := false;
        for j := 1 to F do
          if not buvo[j] and tinka
             then begin
                    buvo[j] := true;
                    inu := i+j-1; { teksto indeksas }
                    inf := j;     { fragmento indeksas }
                    ct := uz[inu];
                    cf := fra[inf];
                    while (inf <= F) and tinka do
                      if (uz[inu] = ct) and (fra[inf] = cf)
                         then begin
                                buvo[inf] := true;
                                inu := inu + 1;
                                inf := inf + 1;
                               end
                         else if (uz[inu] <> ct) and (fra[inf] <> cf)
                               then begin
                                      inu := inu + 1;
                                      inf := inf + 1;
                                    end
                               else tinka := false;
                  end;
        if tinka
           then begin
                  atkoduoti(i);
                  spausdinti;
                  writeln ('TINKA: ', i);
                end;
      end;
  end; { tapatinti }

  procedure skaityti (var F, U: integer; var uz, fra: masyvas);
    var fl: text;
        i: integer;
  begin
    { skaitomas tekstas }
    assign (fl, UZK);
    reset (fl);
    i := 0;
    while not eof (fl) do
      if eoln (fl)
         then readln (fl)
         else begin
                i := i + 1;
                read (fl, uz[i]);
                uz[i] := upcase(uz[i]);
              end;
    U := i;
    close (fl);
    { skaitomas fragmentas }
    assign (fl, FR);
    reset (fl);
    i := 0;
    while not eof (fl) do
      if eoln (fl)
         then readln (fl)
         else begin
                i := i + 1;
                read (fl, fra[i]);
                fra[i] := upcase(fra[i]);
              end;
    F := i;
    close (fl);
  end; { skaityti }
begin
  skaityti (F, U, uz, fra);
  writeln ('Pradeta');
  tapatinti;
  writeln ('Baigta');
end.
